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
uva11624 两个bfs 第一个先处理每个格子起火时间 第二个处理人即可求解 复杂度为o(2rc)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define LL long long
#define N 1000000009
#define maxn 100005
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
int t;
int n,m;
char s[1005][1005];
typedef pair<int,int>pp;
queue<pp>q;
int fire[1005][1005];
int pe[1005][1005];
void bfs_fire()
{
while(!q.empty())
{
pp p=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=p.first+dx[i];
int y=p.second+dy[i];
if(x<1||x>n||y<1||y>m)continue;
if(s[x][y]=='#')continue;
if(fire[x][y]!=-1)continue;
fire[x][y]=fire[p.first][p.second]+1;
q.push(make_pair(x,y));
}
}
}
void bfs_j(int x,int y)
{
q.push(make_pair(x,y));
pe[x][y]=0;
while(!q.empty())
{
pp p=q.front();
if(p.first==1||p.first==n||p.second==1||p.second==m)
{
printf("%d\n",pe[p.first][p.second]+1);
return;
}
q.pop();
for(int i=0;i<4;i++)
{
int x1=p.first+dx[i];
int y1=p.second+dy[i];
if(x1<1||x1>n||y1<1||y1>m)continue;
if(s[x1][y1]=='#')continue;
if(fire[x1][y1]!=-1&&fire[x1][y1]<=pe[p.first][p.second]+1)continue;
if(pe[x1][y1]!=-1)continue;
pe[x1][y1]=pe[p.first][p.second]+1;
q.push(make_pair(x1,y1));
}
}
printf("IMPOSSIBLE\n");
}
int main()
{
int x1,y1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
getchar();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%c",&s[i][j]);
if(s[i][j]=='J'){
x1=i,y1=j;
fire[i][j]=-1;
}
else if(s[i][j]=='F'){
q.push(make_pair(i,j));
fire[i][j]=0;
}
else fire[i][j]=-1;
pe[i][j]=-1;
}
getchar();
}
bfs_fire();
bfs_j(x1,y1);
while(!q.empty())q.pop();
}
return 0;
}