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 Specification
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 squareJ, Joe's initial position in the maze, which is a passable squareF, a square that is on fireThere
will be exactly one J in
each test case.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Output Specification
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.
Output for Sample Input
3
IMPOSSIBLE
/*很难的一道bfs,需要两次广搜,
一次广搜 每个点的最小着火时间,
另一次广搜时如果逃跑的时间大于着火时间,
那么就不能逃跑,就continue,
还有另外一种思路,可以去网上找一下;
*/
#include <iostream>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
struct point {
int x,y,time;
point(int x,int y,int time):x(x),y(y),time(time){}
};
int r,c,sx,sy;
char map[1005][1005];
int dis[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int visited[1005][1005],fire[1005][1005];
void bfs_fire()//广搜每个'.'的最小着火时间,并存储;
{
queue<point> q;
for(int i = 0;i < r;i++)
for(int j = 0;j < c;j++)
if(map[i][j]=='F')
{
visited[i][j]=1;
fire[i][j]=0;
q.push(point(i,j,0));
}
int x,y,time;
while(!q.empty())
{
struct point news=q.front();
q.pop();
for(int i = 0;i < 4; i++)
{
x=news.x+dis[i][0];
y=news.y+dis[i][1];
time=news.time+1;
if(x<0||x>=r||y<0||y>=c||map[x][y]=='#'||visited[x][y]==1||map[x][y]=='F') continue;
visited[x][y]=1;
fire[x][y]=time;
q.push(point(x,y,time));
}
}
}
int bfs()
{
memset(visited,0,sizeof(visited));
queue<point> q;
q.push(point(sx,sy,0));
visited[sx][sy]=1;
int x,y,time;
while(!q.empty())
{
struct point news=q.front();
q.pop();
for(int i = 0;i < 4; i++)
{
x=news.x+dis[i][0];
y=news.y+dis[i][1];
time=news.time+1;
if(x<0||x>=r||y<0||y>=c) return time;//成功逃出
if(map[x][y]=='#'||visited[x][y]==1||map[x][y]=='F'||fire[x][y]<=time) continue;
visited[x][y]=1;
fire[x][y]=time;
q.push(point(x,y,time));
}
}
return 0;//逃不出
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(visited,0,sizeof(visited));
cin>>r>>c;
for(int i=0;i<r;i++) scanf("%s",map[i]);
for(int i = 0;i < r;i++)
for(int j = 0;j < c;j++){
fire[i][j] = INF;
if(map[i][j]=='J')
{
sx=i;
sy=j;
}
}
bfs_fire();
int cnt=bfs();
if(cnt==0) printf("IMPOSSIBLE\n");
else cout<<cnt<<endl;
}
return 0;
}