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
解题思路:
令Joe先走,火焰跟上。广搜,被火焰覆盖的Joe不能继续行动。
代码如下:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <limits.h>
#include <string.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
using namespace std;
const int num = 1005;
const int ma = INT_MAX;
struct node
{
int x,y,t;//第t分钟时位于(x, y)
node(int a,int b,int c):x(a),y(b),t(c){}
node(){}
};
queue<node> qf,qj;//qf存火的坐标,qj存人的
int f[4][2]={{1,0},{0,1},{-1,0},{0,-1}},ff[num][num],fj[num][num];
//ff[][]与fj[][]用于防止重复判断某坐标
char dt[num][num];
int main()//刚开始时间超限,只是因为没有防重复
{
int t;
scanf("%d",&t);
while(t--)
{
int r,c,i,j,s=-1,flag;//s表示第s分钟;
scanf("%d%d",&r,&c);
for(i=1;i<=r;i++)
{
scanf("%s",dt[i]);
for(j=0;j<c;j++)
ff[i][j]=fj[i][j]=0;
}
for(i=1;i<=r;i++)
for(j=0;j<c;j++)
if(dt[i][j]=='J')
{
qj.push(node(i,j,0));
fj[i][j]=1;
}
else if(dt[i][j]=='F')
{
qf.push(node(i,j,0));
ff[i][j]=1;
}
while(1)
{
flag=0;s++;
while(!qj.empty())//第s分钟Joe迈出下一步,达到第s+1分钟的状态
{
node now = qj.front();
if(now.t!=s)//now不是第s分钟的位置
{
flag=-1;
break;
}
qj.pop();
if(dt[now.x][now.y]=='F')//在此处会被蔓延来的火烧si
continue;
for(i=0;i<4;i++)
{
node nex=now;
nex.x+=f[i][0];
nex.y+=f[i][1];
nex.t++;
if(nex.x<1||nex.x>r||nex.y<0||nex.y>=c)//逃生成功
{
flag=1;
break;
}else if(dt[nex.x][nex.y]!='#'&&dt[nex.x][nex.y]!='F'&&!fj[nex.x][nex.y])
{
qj.push(nex);
fj[nex.x][nex.y]=1;
}
}
if(flag)//逃生成功
break;
}
if(flag==0)//Joe不在地图中
{
printf("IMPOSSIBLE\n");
break;
}else if(flag==1)//逃生成功
{
printf("%d\n",s+1);
break;
}
while(!qf.empty())//第s分钟时的火进一步蔓延,达到第s+1分钟的状态
{
node now = qf.front();
if(now.t!=s)
break;
qf.pop();
for(i=0;i<4;i++)
{
node nex=now;
nex.x+=f[i][0];
nex.y+=f[i][1];
nex.t++;
if(nex.x<1||nex.x>r||nex.y<0||nex.y>=c)
continue;
if(!ff[nex.x][nex.y]&&(dt[nex.x][nex.y]=='.'||dt[nex.x][nex.y]=='J'))
{
dt[nex.x][nex.y]='F';
qf.push(nex);ff[nex.x][nex.y]=1;
}
}
}
}
while(!qf.empty())
qf.pop();
while(!qj.empty())
qj.pop();
}
return 0;
}