美文网首页
UVA11624——Fire!

UVA11624——Fire!

作者: xz闲语岁月 | 来源:发表于2017-08-03 23:41 被阅读0次

    Problem

    UVA11624.png

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.
    Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.
    Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

    Input

    The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R,C ≤ 1000.
    The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:
    • #, a wall
    • ., a passable square
    • J, Joe’s initial position in the maze, which is a passable square
    • F, a square that is on fire
    There will be exactly one J in each test case.

    Output

    For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

    Sample Input

    2
    4 4
    ####
    #JF#
    #..#
    #..#
    3 3
    ###
    #J.
    #.F

    Sample Output

    3
    IMPOSSIBLE


    思路

    迷宫问题,BFS即可,技巧在于让火先跑。

    代码

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    char maze[1001][1001];
    int vis[1001][1001];
    int dx[4]={-1,1,0,0};
    int dy[4]={0,0,-1,1};
    int X,Y,sx,sy;
    struct Point{
        int x,y,step,isFire;
        Point(int x,int y,int step,int isFire):x(x),y(y),step(step),isFire(isFire){}
    };
    queue<Point>q;
    
    int BFS(){
        while(!q.empty()){
            Point cur = q.front();
            q.pop();
            for(int i = 0; i < 4; i++){
                int tx = cur.x + dx[i];
                int ty = cur.y + dy[i];
                if(tx < X && tx >= 0 && ty < Y && ty >= 0){
                    if(!vis[tx][ty]){
                        vis[tx][ty] = 1;
                        q.push(Point(tx,ty,cur.step+1,cur.isFire));
                    }
                }
                else if(!cur.isFire) return cur.step+1;
            }
        }
        return -1;
    }
    int main(){
        int k;
        scanf("%d",&k);
        while(k--){
            scanf("%d %d",&X,&Y);
            memset(vis,0,sizeof(vis));
            while(!q.empty()) q.pop();
            for(int i = 0; i < X; ++i){
                scanf("%s",maze[i]);
                for(int j = 0; j < Y; ++j){
                    if(maze[i][j] == 'F'){
                        vis[i][j] = 1;q.push(Point(i,j,0,1));
                    }
                    if(maze[i][j] == 'J') {vis[i][j] = 1;sx=i;sy=j;}
                    if(maze[i][j] == '#') vis[i][j] = 1;
                }
            }
            q.push(Point(sx,sy,0,0));
            int res = BFS();
            if(res == -1) printf("IMPOSSIBLE\n");
            else printf("%d\n",res);
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:UVA11624——Fire!

          本文链接:https://www.haomeiwen.com/subject/rjvmlxtx.html