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
这道题用两个bfs 对火对人。用time标记不能走的路(因为火随时间的变化是固定的)
#include<iostream>
#include<queue>
using namespace std;
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int n,m;
int st_x,st_y;
bool vis[1005][1005];
int times[1005][1005];
int step[1005][1005];
char a[1005][1005];
struct point{
int x,y,s;
P(){}
P(int px,int py,int ps):x(px),y(py),s(ps){}
};
queue<point> q;//对于人的bfs
bool check(int x, int y){
if(x < 0 || x >= n || y < 0 || y >= m)
return false;
if(vis[x][y])
return false;
return a[x][y] == '.';
}
void clear_q(){
while(!q.empty())
q.pop();
}
void bfs_time()//对于火的bfs
{
while(!q.empty()){
P cur = q.front();
q.pop();
for(int i = 0; i < 4; ++i){
int nx = cur.x + dir[i][0];
int ny = cur.y + dir[i][1];
if(check(nx,ny)){
vis[nx][ny] = true;
times[nx][ny] = times[cur.x][cur.y] + 1;
q.push(P(nx,ny,times[nx][ny]));
}
}
}
}
int bfs(int x, int y){
step[x][y] = 0;
q.push(P(x,y,0));
vis[x][y] = true;
while(!q.empty()){
P cur = q.front();
q.pop();
if(cur.x == 0 || cur.x == n-1 || cur.y == 0 || cur.y == m-1){
return step[cur.x][cur.y] + 1;
}
for(int i = 0; i < 4; ++i){
int nx = cur.x + dir[i][0];
int ny = cur.y + dir[i][1];
if(check(nx,ny) && step[cur.x][cur.y] + 1 < times[nx][ny]){
step[nx][ny] = step[cur.x][cur.y] + 1;
vis[nx][ny] = true;
q.push(P(nx,ny,step[nx][ny]));
}
}
}
return -1;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
cin >> n >> m;
memset(times,INF,sizeof(times));
memset(vis,false,sizeof(vis));
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cin >> a[i][j];
if(a[i][j] == 'J'){
st_x = i;
st_y = j;
}
if(a[i][j] == 'F'){
vis[i][j] = true;
q.push(P(i,j,0));
times[i][j] = 0;
}
}
}
bfs_time();
clear_q();
memset(vis,false,sizeof(vis));
int ans = bfs(st_x,st_y);
if(ans == -1){
cout << "IMPOSSIBLE" <<endl;
}
else{
cout << ans << endl;
}
}
return 0;
}