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
题意很好理解迷宫起火了,J要逃出迷宫,F代表起火点,火势会蔓延,问J能否逃出迷宫。
先BFS起火点,再BFSJ的起点,只要J的逃跑时间小于火势蔓延的时间就能逃出来。
但是要注意两点:
起火点有多个 题目中的是复数形式
要先把起火点全部入队 然后广搜,不要找到一个起火点广搜一次。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<iostream>
#define ll long long
#define ull unsigned long long
const int N =1e9+7;
const int M=1e3+5;
using namespace std;
int i,j,k,l,n,m,r;
char a[M][M]; //存放地图
int b[4][2]= {1,0,-1,0,0,1,0,-1}; // 方向数组
int c[M][M],w[M][M]; // 标记数组
struct node
{
int x,y,step;
};
queue<node>q;
void init()
{
for(i=0; i<1000; i++)
{
for(j=0; j<1000; j++)
{
c[i][j]=N;
}
}
memset(w,0,sizeof(w));
}
bool check(int x,int y)
{
return x>=0&&y>=0&&x<n&&y<m&&a[x][y]!='#'&&!w[x][y];
}
void bfs1()
{
node g,h;
while(!q.empty())
{
h=q.front();
q.pop();
for(int i=0; i<4; i++)
{
g.x=h.x+b[i][0];
g.y=h.y+b[i][1];
g.step=h.step+1;
if(check(g.x,g.y))
{
w[g.x][g.y]=1;
q.push(g);
c[g.x][g.y]=min(c[g.x][g.y],g.step); //记录火势蔓延到该点的最小时间
}
}
}
}
void bfs2(node f)
{
node g;
memset(w,0,sizeof(w));
while(!q.empty()) q.pop();
q.push(f);
w[f.x][f.y]=1;
while(!q.empty())
{
f=q.front();
q.pop();
if(f.x==0||f.y==0||f.x==n-1||f.y==m-1) //到达边界
{
r=min(r,f.step+1);
// return ;
}
for(int i=0; i<4; i++)
{
g.x=f.x+b[i][0];
g.y=f.y+b[i][1];
g.step=f.step+1;
if(check(g.x,g.y)&&g.step<c[g.x][g.y]) //J的逃跑时间要小于火势蔓延的时间
{
w[g.x][g.y]=1;
q.push(g);
}
}
}
}
int main()
{
int T,jx,jy;
node t;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
r=N;
init();
for(i=0; i<n; i++) scanf("%s",a[i]);
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(a[i][j]=='F')
{
t.x=i,t.y=j,t.step=0;
c[t.x][t.y]=0;
w[t.x][t.y]=1;
q.push(t); //起火点入队
}
if(a[i][j]=='J') jx=i,jy=j; //记录起点
}
}
bfs1();
t.x=jx,t.y=jy,t.step=0;
bfs2(t);
if(r==N) printf("IMPOSSIBLE\n");
else printf("%d\n",r);
}
return 0;
}