UVA-11624 Fire!

Description

Jane is one of the most talented young programmers as well as an astrophysicist. Recently she discovered a planet and named it Jotunheim - the world of giants. As you already guessed that the inhabitants are all giants. Among them the Frost Giants are the most evil ones. Before Jane could publicly announce her great discovery, the Frost Giants came and captured her in a maze. Since the Giants would be discovered to the universe because of her, that's why they lit fires on some positions in the maze to kill her.

You are given Jane's location in the maze and the positions of the fires lit by the Frost Giants whom are always keeping an eye on her; you must find out whether Jane can escape from the maze before fire catches her, and how fast she can do it.

The Maze is defined as a 2D grid and the locations are defined as squares. The cost of each move is one square per minute. In each move, Jane can move vertically or horizontally but not diagonally. She cannot move to a square which is blocked by an obstacle, or which is already burning. If a square has fire in it, in the next minute, fires spread to its adjacent non-obstacle squares (vertically or horizontally). Jane can escape from the maze from any squares that borders the edge of the maze.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R, C ≤ 200. 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:

1.      #         an obstacle

2.      .           a free location

3.      J           Jane's initial position in the maze (there will be exactly one 'J' in the maze)

4.      F          Position of a Fire

Output

For each case, print the case number and 'IMPOSSIBLE' if Jane cannot escape from the maze before fire reaches her, or the earliest time for Jane to safely escape from the maze, in minutes.

Sample Input

2

4 5

##.##

#JF.#

#...#

#...#

3 3

###

#J.

#.F

Sample Output

Case 1: 3

Case 2: IMPOSSIBLE


題解:先把着火BFS,知道每個J可以走的格子着火時間,然後把J再進行一次BFS,利用着火時間作對比


#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 205;
int R,C;
char pic[MAXN][MAXN];
int timeF[MAXN][MAXN]; //火燃燒周圍地方需要的時間
int timeJ[MAXN][MAXN]; //J走地方需要的時間
int direct[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; //方向轉向
typedef pair<int,int> pi;
queue<pi> Q; //存儲隊列
pi startJ;   //開始J
void bfsfireAllPic(){  //寬度遍歷所有位置所需要的時間
    while(!Q.empty()){
        pi u = Q.front(); Q.pop();
        for(int i = 0; i < 4; i++){
            int x = u.first+direct[i][0];
            int y = u.second+direct[i][1];
            if(x<0||x>=R||y<0||y>=C)continue;
            if(timeF[x][y]!=-1)continue;
            if(pic[x][y]=='#')continue;
            timeF[x][y] = timeF[u.first][u.second]+1;
            Q.push(make_pair(x,y));
        }
    }
}
void bfs_J(int case_time){ //通過F燃燒周圍各需的時間利用,寬度搜索J可走位置找最短路
    while(!Q.empty())Q.pop();
    Q.push(startJ);
    while(!Q.empty()){
        pi u = Q.front();
        if(u.first==0||u.second==0||u.first==R-1||u.second==C-1){
            printf("Case %d: %d\n",case_time,timeJ[u.first][u.second]+1);
            return;
        }
        Q.pop();
        for(int i = 0; i < 4; i++){
            int x = u.first+direct[i][0];
            int y = u.second+direct[i][1];
            if(pic[x][y] == '#')continue;
            if(timeJ[x][y] != -1)continue;
            if(x<0||x>=R||y<0||y>=C)continue;
            //當移到下一個位置,J到的時間比F到的時間長,就不能移動到所在位置
            if(timeF[x][y]!=-1 && timeJ[u.first][u.second]+1>=timeF[x][y])continue;
            timeJ[x][y] = timeJ[u.first][u.second] + 1;
            Q.push(make_pair(x,y));
        }
    }
    printf("Case %d: IMPOSSIBLE\n",case_time);
}

int main(){
    int T;
    scanf("%d",&T);
    for(int i = 1; i <= T ; i++){
        while(!Q.empty())Q.pop();
        scanf("%d%d",&R,&C);
        for(int i = 0;i < R;i++){
            scanf("%s",pic[i]);
            for(int j = 0;j < C;j++){
                pic[i][j]=='J'?startJ = make_pair(i,j),timeJ[i][j] = 0 : timeJ[i][j] = -1;
                pic[i][j]=='F'? Q.push(make_pair(i,j)),timeF[i][j] = 0 : timeF[i][j] = -1;
            }
        }
        bfsfireAllPic();
        bfs_J(i);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章