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;
}