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
There will be exactly one J in each test case.
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
'#'的地方是墙,'.'是草,'F'是火,火每秒和人每秒走一个格子,问至少需要多少秒从J开始逃离
BFS,每次先更新火,再更新走的位置就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 1005
struct node{
int x,y,t;
int op;
node(int xx,int yy,int tt,int oo):x(xx),y(yy),t(tt),op(oo){}
};
int a[N][N];
bool vis[N][N];
int jx,jy,fx,fy;
int n,m;
bool check(int x,int y){
if (x<0 || x>=n)
return false;
if (y<0 || y>=m)
return false;
return true;
}
int vx[]={0,0,1,-1};
int vy[]={1,-1,0,0};
void bfs(){
queue<node> q;
memset(vis,false,sizeof(vis));
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++){
getchar();
for (int j=0;j<m;j++){
a[i][j]=getchar();
if (a[i][j]=='J'){
a[i][j]='.';
jx=i; jy=j;
}
if (a[i][j]=='F'){
a[i][j]='#';
q.push(node(i,j,0,1));
}
}
}
q.push(node(jx,jy,0,2));
vis[jx][jy]=true;
while (!q.empty()){
node temp=q.front();
q.pop();
int x=temp.x;
int y=temp.y;
int t=temp.t;
int op=temp.op;
//cout<<x<<' '<<y<<' '<<t<<' '<<op<<endl;
if (op==1){
for (int i=0;i<4;i++){
int tx=x+vx[i];
int ty=y+vy[i];
if (check(tx,ty) && a[tx][ty]=='.'){
a[tx][ty]='#';
q.push(node(tx,ty,t+1,1));
}
}
}else{
for (int i=0;i<4;i++){
int tx=x+vx[i];
int ty=y+vy[i];
if (check(tx,ty) && !vis[tx][ty] && a[tx][ty]=='.'){
vis[tx][ty]=true;
q.push(node(tx,ty,t+1,2));
}else{
if (!check(tx,ty)){
printf("%d\n",t+1);
return;
}
}
}
}
}
printf("IMPOSSIBLE\n");
}
int main(){
int t;
scanf("%d",&t);
while (t--){
bfs();
}
return 0;
}
/*
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
*/