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 思想是 火走一步,然后人再走一步。需要注意的是有可能不只一个火; 我的方法比较麻烦,但是很容易理解;#include#include #include using namespace std;char map[1005][1005];//地图int vis1[1005][1005];//记录人的路径int vis2[1005][1005];//记录火的路径int go[4][2]= { { 0,1},{ 0,-1},{ 1,0},{-1,0}};//四个方向搜索int ji,jj;int n,m;struct node1//用来存储人的路径{ int si,sj,ans;};struct node2//这个是用来转换人的路径的{ int si2,sj2,ans2;};struct node3//用来存储火的路径{ int di,dj;};struct node4//这个用来转化火的路径的{ int di2,dj2;};node2 now2,next2;queue s2;node4 now4,next4;queue s4;node1 now1,next1;queue s1;node3 now3,next3;queue s3;int bfs(int si,int sj){ now1.si=si,now1.sj=sj,now1.ans=0;//把人的起点压入队列 s1.push(now1); while(!s1.empty())//如果人没路可走,跳出循环 { while(!s3.empty())//如果火走完了,跳出 { now3=s3.front(); for(int u3=0; u3<4; u3++)//四个方向广搜 { int x3=now3.di+go[u3][0],y3=now3.dj+go[u3][1]; if(x3>=0&&y3>=0&&x3 =0&&y1>=0&&x1 >t; while(t--) { memset(vis1,0,sizeof(vis1)); memset(vis2,0,sizeof(vis2)); cin>>n>>m; for(int i=0; i >map[i][j]; if(map[i][j]=='J')//记录人的起点 { ji=i; jj=j; vis1[ji][jj]=1; } if(map[i][j]=='F')//找到火并压入队列 { now3.di=i; now3.dj=j; vis2[now3.di][now3.dj]=1; s3.push(now3); } } } if(ji==0||jj==0||ji==n-1||jj==m-1) { cout<<"1"<