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 <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int x,y;
int step;
node(int x=,int y=) : x(x),y(y) {} //构造函数
};
const int inf=0x3f3f3f;
int to[][]={,,,,,-,-,},t[][],n,m,sx,sy,ou;
bool vit[][];
char a[][];
void bfs1()
{
queue<node>que;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(a[i][j]=='F')
{
t[i][j]=;
que.push(node(i,j));
}
}
}
while(!que.empty())
{
node exa=que.front();
que.pop();
for(int i=;i<;i++)
{
int tx=exa.x+to[i][];
int ty=exa.y+to[i][]; if(a[tx][ty]=='#') continue;
if(tx<||tx>n||ty<||ty>m) continue;
if(t[exa.x][exa.y]+>=t[tx][ty]) continue;//不用vis,因为时间短的优先考虑,重点!!! t[tx][ty]=t[exa.x][exa.y]+;//记录时间
que.push(node(tx,ty));
}
}
} void bfs2()
{
node ee;
queue<node>que;
ee.x=sx;
ee.y=sy;
ee.step=;
que.push(ee);
vit[ee.x][ee.y]=;
while(!que.empty())
{
ee=que.front();
node zz;
que.pop();
if(ee.x==||ee.y==||ee.x==n||ee.y==m)
{
ou=ee.step+;
return ;
}
for(int i=;i<;i++)
{ zz.x=ee.x+to[i][];
zz.y=ee.y+to[i][];
zz.step=ee.step+; //step记录每一个位置所用的时间与火的时间作比较 if(zz.x<||zz.y<||zz.x>n||zz.y>m) continue;
if(a[zz.x][zz.y]=='#') continue;
if(vit[zz.x][zz.y]) continue;
if(zz.step>=t[zz.x][zz.y]) continue; vit[zz.x][zz.y]=;
que.push(zz);
}
}
} int main()
{
int c;
cin>>c;
while(c--)
{
memset(vit,,sizeof(vit));
memset(t,inf,sizeof(t));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",a[i]+);
for(int j=;j<=m;j++)
{
if(a[i][j]=='J'){sx=i;sy=j;}
}
}
ou=inf;
bfs1();
bfs2();
if(ou>=inf) printf("IMPOSSIBLE\n");
else printf("%d\n",ou);
}
return ;
}
05-11 19:23