这道题目需要进行两次广搜,一次是对火的,一次是对Joe的,只要保证Joe在到达那点时火灾没有蔓延到,同时要注意火灾可能有多处(可能也存在没有火灾的情况),在写代码的时候只要Joe到达边界即可判定他成功逃脱,而不是他越过边界后才停止循环,我的两次Runtime Error就是这么造成的
Problem B: Fire!
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 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 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
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct list
{
int x;
int y;
int step;
};
bool checkFire(int x,int y);
bool checkJoe(int x,int y,int t);
bool check(int x,int y);
char map[1001][1001];
int time[1001][1001];
bool book[1001][1001];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int r,c;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
queue<struct list> p,q;
struct list n,F;
memset(map,0,sizeof(map));
memset(time,-1,sizeof(time));
memset(book,false,sizeof(book));
scanf("%d%d",&r,&c);
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
scanf(" %c",&map[i][j]);
if(map[i][j]=='J')
{
n.x=i;
n.y=j;
n.step=0;
q.push(n);
}
if(map[i][j]=='F')
{
F.x=i;
F.y=j;
F.step=0;
time[i][j]=0;
p.push(F);
}
}
}
while(!p.empty())
{
struct list P;
P=p.front();
p.pop();
for(int i=0;i<4;i++)
{
if(checkFire(P.x+dir[i][0],P.y+dir[i][1]))
{
F.x=P.x+dir[i][0];
F.y=P.y+dir[i][1];
F.step=P.step+1;
time[P.x+dir[i][0]][P.y+dir[i][1]]=P.step+1;
p.push(F);
}
}
}
while(!q.empty()&&!check(q.front().x,q.front().y))
{
struct list Q;
Q=q.front();
for(int i=0;i<4;i++)
{
if(checkJoe(Q.x+dir[i][0],Q.y+dir[i][1],Q.step+1))
{
book[Q.x+dir[i][0]][Q.y+dir[i][1]]=true;
n.x=Q.x+dir[i][0];
n.y=Q.y+dir[i][1];
n.step=Q.step+1;
q.push(n);
}
}
q.pop();
}
if(q.empty())
printf("IMPOSSIBLE\n");
else
printf("%d\n",q.front().step+1);
}
return 0;
}
bool checkFire(int x,int y)
{
if(x>=0&&x<r&&y>=0&&y<c)
if(time[x][y]==-1)
if(map[x][y]=='.'||map[x][y]=='J')
return true;
return false;
}
bool checkJoe(int x,int y,int t)
{
if(t<time[x][y]||time[x][y]==-1)
if(book[x][y]==false)
if(map[x][y]=='.')
return true;
return false;
}
bool check(int x,int y)
{
if(x==0||x==r-1||y==0||y==c-1)
return true;
return false;
}