Description

Problem B: Fire!

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 (notdiagonally). The fire spreads all four directions from each square that is onfire. 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, separatedby spaces, with 1 <= R, C <= 1000. The following R lines of the test caseeach 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
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 beforethe fire reaches him, or an integer giving the earliest time Joe can safely exit themaze, in minutes.Output for Sample Input
3 IMPOSSIBLE
代码:
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
const int maxx=1005;
int g[maxx][maxx];
bool visit[maxx][maxx];
int r,c;
int mintime;
int jx,jy,fx[1005],fy[1005];
int cnt;
struct node
{
int x,y;
int step;
} s,t;
void bfsF()
{
queue<struct node> p;
struct node cur;
for(int i=0;i<cnt;i++)
{
t.x=fx[i];
t.y=fy[i];
t.step=0;
g[fx[i]][fy[i]]=0;
p.push(t);
visit[fx[i]][fy[i]]=true;
}
while(!p.empty())
{
cur=p.front();
p.pop();
int xx=cur.x;
int yy=cur.y;
g[xx][yy]=cur.step;
if(xx-1>=0&&g[xx-1][yy]==-1&&!visit[xx-1][yy])
{
visit[xx-1][yy]=true;
s.x=xx-1;
s.y=yy;
s.step=cur.step+1;
p.push(s);
}
if(xx+1<r&&g[xx+1][yy]==-1&&!visit[xx+1][yy])
{
visit[xx+1][yy]=true;
s.x=xx+1;
s.y=yy;
s.step=cur.step+1;
p.push(s);
}
if(yy-1>=0&&g[xx][yy-1]==-1&&!visit[xx][yy-1])
{
visit[xx][yy-1]=true;
s.x=xx;
s.y=yy-1;
s.step=cur.step+1;
p.push(s);
}
if(yy+1<c&&g[xx][yy+1]==-1&&!visit[xx][yy+1])
{
visit[xx][yy+1]=true;
s.x=xx;
s.y=yy+1;
s.step=cur.step+1;
p.push(s);
}
}
}
int bfsJ(int x,int y)
{
queue<struct node> q;
s.x=x;
s.y=y;
s.step=0;
q.push(s);
while(!q.empty())
{
struct node cur=q.front();
q.pop();
int xx=cur.x;
int yy=cur.y;
if(xx-1<0||xx+1>=r||yy-1<0||yy+1>=c)
{
return cur.step+1;
}
if((g[xx-1][yy]==-1||g[xx-1][yy]-cur.step>=2)&&!visit[xx-1][yy])
{
visit[xx-1][yy]=true;
s.x=xx-1;
s.y=yy;
s.step=cur.step+1;
q.push(s);
}
if((g[xx+1][yy]==-1||g[xx+1][yy]-cur.step>=2)&&!visit[xx+1][yy])
{
visit[xx+1][yy]=true;
s.x=xx+1;
s.y=yy;
s.step=cur.step+1;
q.push(s);
}
if((g[xx][yy-1]==-1||g[xx][yy-1]-cur.step>=2)&&!visit[xx][yy-1])
{
visit[xx][yy-1]=true;
s.x=xx;
s.y=yy-1;
s.step=cur.step+1;
q.push(s);
}
if((g[xx][yy+1]==-1||g[xx][yy+1]-cur.step>=2)&&!visit[xx][yy+1])
{
visit[xx][yy+1]=true;
s.x=xx;
s.y=yy+1;
s.step=cur.step+1;
q.push(s);
}
}
return -1;
}
int main()
{
int T;
char ch;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&r,&c);
getchar();
cnt=0;
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
scanf("%c",&ch);
if(ch=='J')
{
jx=i;
jy=j;
g[i][j]=0;
}
else if(ch=='F')
{
fx[cnt]=i;
fy[cnt++]=j;
g[i][j]=0;
}
else if(ch=='.')
{
g[i][j]=-1;
}
else
g[i][j]=-2;
}
getchar();
}
mintime=-1;
for(int i=0; i<=r; i++)
{
for(int j=0; j<=c; j++)
visit[i][j]=false;
}
bfsF();
for(int i=0; i<=r; i++)
{
for(int j=0; j<=c; j++)
visit[i][j]=false;
}
mintime=bfsJ(jx,jy);
if(mintime==-1)
{
printf("IMPOSSIBLE\n");
}
else printf("%d\n",mintime);
}
return 0;
}