题目:
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
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
Output for Sample Input
3 IMPOSSIBLE
题意:在一个迷宫中Joe要从初始位置跑出地图(到达边界即成功),在他跑的同时火也在扩散。火烧的方向,Joe跑的方向都可以为上下左右,每秒走一步。他必须在火到达他的当前位置之前跑出去,问他是否可以跑出地图,跑出去的最早时间是多少。
#是墙
.是可行路径
J是Joe的初始位置
F是火的初始位置
思路:BFS两次。第一次求火烧到所有可以到达的位置的最小时间,第二次BFS求Joe在火烧的同时(到达某一位置时火烧到的时间一定要比其大)是否可以到达边界·,以及逃出迷宫的时间。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAXN = 1010;
int n,m;
char G[MAXN][MAXN];
int fire[MAXN][MAXN]; //火烧到(i,j)的最小时间
int Time[MAXN][MAXN]; //人到达(i,j)的最小时间
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
void bfsFire(){ //求火烧到(i,j)的最小时间
memset(fire,-1,sizeof(fire));
queue< pair<int,int> > q;
for(int i = 0 ; i<n ; i++){
for(int j = 0 ; j<m ; j++){
if(G[i][j] == 'F'){
fire[i][j] = 0; //F为火的初始位置,时间为0
q.push(make_pair(i,j));
}
}
}
while(!q.empty()){
pair<int,int> temp = q.front();
q.pop();
int x = temp.first; int y = temp.second;
for(int i = 0 ; i<4 ; i++){
int nxtx = x+dir[i][0];
int nxty = y+dir[i][1];
if(nxtx<0 || nxtx>=n || nxty<0 || nxty>=m) //地图之外
continue;
if(fire[nxtx][nxty]!=-1) //火之前已经烧到
continue;
if(G[nxtx][nxty] == '#') //是墙
continue;
fire[nxtx][nxty] = fire[x][y] + 1;
q.push(make_pair(nxtx,nxty));
}
}
}
int bfs(){ //求人跑出迷宫的最小时间
queue< pair<int,int> > q;
memset(Time,-1,sizeof(Time));
for(int i = 0 ; i<n ; i++){
for(int j = 0 ; j<m ; j++){
if(G[i][j] == 'J'){
q.push(make_pair(i,j));
Time[i][j] = 0; //J为JOE逃跑的起始位置,时间为0
}
}
}
while (!q.empty()){
pair<int,int> temp = q.front();
q.pop();
int x = temp.first;
int y = temp.second;
if(x==0 || y==0 || x==n-1 || y==m-1) //能顺利到达边界即为逃出边界。逃出去的时间要+1,即从边界跑出去
return Time[x][y]+1;
for(int i = 0 ; i<4 ; i++){
int nxtx = x+dir[i][0];
int nxty = y+dir[i][1];
if(Time[nxtx][nxty] != -1) //之前已经搜索过
continue;
if(nxtx<0 || nxtx>=n || nxty<0 || nxty>=m) //下一步在规定范围之外
continue;
if(G[nxtx][nxty] == '#') //下一步是墙
continue;
if(fire[nxtx][nxty]!=-1 && Time[x][y]+1>=fire[nxtx][nxty]) //人最小到达的时间比火烧到的时间大
continue;
Time[nxtx][nxty] = Time[x][y] + 1;
q.push(make_pair(nxtx,nxty));
}
}
return -1;
}
int main(){
ios::sync_with_stdio(false);
int T;
scanf("%d",&T);
while (T--){
scanf("%d %d",&n,&m);
for(int i = 0 ; i<n ; i++){
scanf("%s",G[i]);
}
bfsFire();
int ans = bfs();
if(ans == -1)
printf("IMPOSSIBLE\n");
else
printf("%d\n",ans);
}
return 0;
}