UVA 11624 Fire!

来源:互联网 发布:android系统相册源码 编辑:程序博客网 时间:2024/06/05 19:55

题意:在一个森林中有某些地方起火,有一个在森林中要逃出森林,到矩阵边缘即算逃出森林,火蔓延的速度和逃跑速度相同,判断这人是否能够逃出森林

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28833

思路:对所有起火点进行广搜,记录每个点的最短时间,第二次对人进行广搜,判断到底边界时是否满足条件

注意点:起火点可能有多个


以下为AC代码:

RunID
User
OJ
All
Prob ID
Result
All
Memory
(KB)
Time
(ms)
Language
All
Length
(Bytes)
Submit Time
3390675
luminous11
UVA
11624Accepted 335
C++11 4.8.2
3092
2015-02-28 16:56:42

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <vector>#include <deque>#include <list>#include <cctype>#include <algorithm>#include <climits>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#include <iomanip>#include <cstdlib>#include <ctime>#define ll long long#define ull unsigned long long#define all(x) (x).begin(), (x).end()#define clr(a, v) memset( a , v , sizeof(a) )#define pb push_back#define mp make_pair#define read(f) freopen(f, "r", stdin)#define write(f) freopen(f, "w", stdout)using namespace std;const double pi = acos(-1);int dir[4][2] = { 0,1, 0,-1, 1,0, -1,0 };int adj[1005][1005];char str[1005][1005];bool vis[1005][1005];int m, n;struct node{    int x, y, cnt;    node(){}    node ( int _x, int _y ) : x(_x), y(_y) {}    node ( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}};queue<node> q;void init(){    clr( str, '@' );    for ( int i = 1; i <= m; i ++ ){        scanf ( "%s", &str[i][1] );        str[i][n+1] = '@';    }    clr ( adj, 0x3f3f3f3f );}void bfs ( ){    while ( ! q.empty() ){        node tmp = q.front();        q.pop();        for ( int i = 0; i < 4; i ++ ){            int xi = tmp.x + dir[i][0];            int yi = tmp.y + dir[i][1];            if ( xi < 0 || yi < 0 || xi > m + 1 || yi > n + 1 )continue;            if ( adj[xi][yi] > adj[tmp.x][tmp.y] + 1 ){                adj[xi][yi] = adj[tmp.x][tmp.y] + 1;                if ( str[xi][yi] != '#' && str[xi][yi] != '@' )                    q.push ( node ( xi, yi ) );            }        }    }}int bfs2 ( int x, int y ){    queue<node> q;    clr ( vis, 0 );    vis[x][y] = 1;    q.push ( node ( x, y, 1 ) );    while ( ! q.empty() ){        node tmp = q.front();        q.pop();        for ( int i = 0; i < 4; i ++ ){            node now = node( tmp.x + dir[i][0], tmp.y +dir[i][1], tmp.cnt + 1 );            if ( vis[now.x][now.y] == 0 && adj[now.x][now.y] > now.cnt && str[now.x][now.y] != '#' ){                if ( str[now.x][now.y] == '@' ){                    return tmp.cnt;                }                vis[now.x][now.y] = 1;                q.push ( now );            }        }    }    return -1;}void print(){    for ( int i = 0; i <= 5; i ++ ){        for ( int j = 0; j <= 5; j ++ ){            cout << str[i][j];        }        cout << endl;    }}void solve(){    int sx, sy;    for ( int i = 1; i <= m; i ++ ){        for ( int j = 1; j <= n; j ++ ){            if ( str[i][j] == 'J' ){                sx = i;                sy = j;            }            if ( str[i][j] == 'F' ){                q.push ( node ( i, j ) );                adj[i][j] = 1;            }        }    }    int ans = 1 << 29;    bfs ();    ans = bfs2 ( sx, sy );    if ( ans == -1 )        printf ("IMPOSSIBLE\n");    else        printf ( "%d\n", ans );}int main(){    int t;    scanf ( "%d", &t );    while ( t -- ){        scanf ( "%d%d", &m, &n );        init();        solve();    }    return 0;}


0 0