Fire!
Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.
Input Specification
The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:
· #, a wall
· ., a passable square
· J, Joe's initialposition in the maze, which is a passable square
· F, a square that is onfire
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 cannotexit the maze before the fire reaches him, or an integer giving the earliesttime Joe can safely exit the maze, in minutes.
Output for SampleInput
3
IMPOSSIBLE
分析:其实这道题若是没有火,就是一个非常简单的迷宫题,那么加上了火之后呢,其实也没有那么难。要知道,
火是固定的,那么他能烧到的地方也是确定的,但是人就不一样了,人是根据火的路线来确定路线的,所以到此为止,就要明白要进行两次bfs,一次对火进行预处理,求出火到达这个格子的时间,第二次对人进行处理,如果人到达这个格子的时间早于火,那么就可以纳入队列,结束条件是人到达边界
一个比较注意的地方就是起火点有多个!
import java.util.*;
public class Main {
static Scanner in = new Scanner(System.in);
static char[][] maze = new char[1005][1005];
static boolean[][] vis = new boolean[1005][1005];
static Queue<Node> qf = new LinkedList<Node>();
static Queue<Node> q = new LinkedList<Node>();
static int[][] sp1 = new int[1005][1005];
static int[][] sp2 = new int[1005][1005];
static int r,c,sum;
static int[][] dir={{0,1},{1,0},{0,-1},{-1,0}};
static void bfsFire(Queue<Node> q){
int tx,ty;
while(!q.isEmpty()){
Node tem = q.poll();
vis[tem.x][tem.y]=true;
Node nw = new Node();
for (int i = 0; i < 4; i++) {
tx=tem.x+dir[i][0];
ty=tem.y+dir[i][1];
if(tx<0||ty<0||tx>=r||ty>=c)
continue;
if(!vis[tx][ty]&&(maze[tx][ty]=='.'||maze[tx][ty]=='J')){
vis[tx][ty]=true;
nw.x=tx;
nw.y=ty;
sp1[tx][ty]=sp1[tem.x][tem.y]+1;
q.add(nw);
}
}
}
}
static void bfs(Queue<Node> q){
int tx,ty;
boolean f=false;
while(!q.isEmpty()){
Node tem = q.poll();
vis[tem.x][tem.y]=true;
Node nw = new Node();
for (int i = 0; i < 4; i++) {
tx=tem.x+dir[i][0];
ty=tem.y+dir[i][1];
if(tx<0||ty<0||tx>=r||ty>=c)
continue;
if(!vis[tx][ty]&&maze[tx][ty]=='.'){
vis[tx][ty]=true;
nw.x=tx;
nw.y=ty;
sp2[tx][ty]=sp2[tem.x][tem.y]+1;
if(sp2[tx][ty]<sp1[tx][ty])
sum++;
q.add(nw);
}
if((tx==0||ty==0||tx==r-1||ty==c-1)&&maze[tx][ty]!='#')
f=true;
}
if(f)
break;
}
}
public static void main(String[] args) {
int k = in.nextInt();
int x=0,y=0;
while(k-->0){
qf.clear();
q.clear();
r = in.nextInt();
c = in.nextInt();
String s;
for (int i = 0; i < r; i++) {
s=in.next();
for (int j = 0; j <s.length(); j++) {
maze[i][j]=s.charAt(j);
vis[i][j]=false;
sp1[i][j]=0;
sp2[i][j]=0;
if(maze[i][j]=='J')
{x=i;y=j;}
if(maze[i][j]=='F'){
Node f= new Node(i, j);
qf.add(f);
}
}
}
bfsFire(qf);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
vis[i][j]=false;
}
}
Node p =new Node(x, y);
q.add(p);
sum=0;
bfs(q);
if(sum!=0)
System.out.println((sum+1));
else
System.out.println("IMPOSSIBLE");
}
}
}
class Node{
int x;
int y;
Node(){}
Node(int x,int y){
this.x=x;
this.y=y;
}
}