UVA 11624 Fire!

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

Input

The first line of input contains a single integer, the number of testcases to follow. The first line of each test case contains the twointegers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. Thefollowing R lines of the test case each contain one row of the maze. Each of these lines contains exactlyC 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 fireThere will be exactly one J in each test case.OutputFor each test case, 

output

 a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before thefire 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的题目,大意是如果一个人可以在火烧到自己没法再走之前能否到达边界的问题。

思路是先考虑火的蔓延方向,然后搜索人能到达的地方。最后当被火包围的时候就失败了,反之如果达到边界就成功了。我们维护一个结构体,里面有坐标和当前是操作人还是火。用一个BFS即可

/*************************************************************************
	> File Name: Fire.cpp
	> Author: Zhanghaoran0
	> Mail: chiluamnxi@gmail.com
	> Created Time: 2015年08月15日 星期六 09时48分20秒
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int T;
int R, C;
char map[1005][1005];
int sx = -1, sy = -1;
struct node{
    int x;
    int y;
    bool form;
    int step;
}q[1000010];

bool flag[1005][1005];
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};

int bfs(){
    int head = 0;
    int tail = 0;

    memset(flag, false, sizeof(flag));
    for(int i = 0; i < R; i ++)
        for(int j = 0; j < C; j ++)
            if(map[i][j] == 'F'){
                q[tail].x = i;
                q[tail].y = j;
                q[tail].form = false;
                q[tail].step = 0;
                tail ++;
            }   

    q[tail].x = sx;
    q[tail].y = sy;
    q[tail].step = 0;
    q[tail].form = true;
    tail ++;
    while(head < tail){
        if(!q[head].form){
            for(int i = 0; i < 4; i ++){
                int tempx = q[head].x + dx[i];
                int tempy = q[head].y + dy[i];
                if(tempx < 0 || tempx >= R || tempy < 0 || tempy >= C || map[tempx][tempy] == '#' || map[tempx][tempy] == 'F')
                    continue;
                map[tempx][tempy] = 'F';
                q[tail].x = tempx;
                q[tail].y = tempy;
                q[tail].form = false;
                q[tail].step = q[head].step + 1;
                tail ++;
            } 
        }

        else{
            for(int i = 0; i < 4; i ++){
                int tempx = q[head].x + dx[i];
                int tempy = q[head].y + dy[i];
                if(flag[tempx][tempy] || map[tempx][tempy] == '#' || map[tempx][tempy] == 'F')
                    continue;
                if(tempx < 0 || tempx >= R || tempy < 0 || tempy >= C)
                    return q[head].step + 1;
                flag[tempx][tempy] = true;
                q[tail].x = tempx;
                q[tail].y = tempy;
                q[tail].form = true;
                q[tail].step = q[head].step + 1;
                tail ++;
            }
        }
        head ++;
    }
    return 0;
}


int main(void){
    cin >> T;
    while(T --){
        cin >> R >> C;
        sx = -1;
        sy = -1;
        int ans = 0;
        for(int i = 0; i < R; i ++){
            scanf("%s", map[i]);
            if(sx == -1 && sy == -1){
                for(int j = 0; j < C; j ++)
                    if(map[i][j] == 'J'){
                        sx = i;
                        sy = j;
                        break;
                    }
            }
        }
        ans = bfs();
        if(ans)
            printf("%d\n", ans);
        else 
            printf("IMPOSSIBLE\n");
    }
    return 0;

}


posted @ 2015-08-17 16:19  ChiLuManXi  阅读(146)  评论(0编辑  收藏  举报