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
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帮助joe走出一个大火蔓延的迷宫,joe每分钟走一格,大火每分钟蔓延一格,“F”代表fire,“#”代表障碍物,“J”代表joe的初始位置,“.”代表空地。
大致思路,先预处理大火到某个空地的时间,在BFS joe路径时加个判断,在joe到这里之前,大火烧到这里没有。
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <ctime>
#include <vector>
#include <cstring>
#include <map>
#include <string>
#include <queue>
using namespace std;
#define LL long long
#define ULL unsigned long long
//#define REP(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define INFLL (1LL)<<62
#define mset(a) memset(a,0,sizeof a)
#define FR(a) freopen(a,"r",stdin)
#define FW(a) freopen(a,"w",stdout)
#define PI 3.141592654
const LL MOD = 1000000007;
const int maxn = 1011;
queue<pair<int,int> > q;
int moveto[][2]={{-1,0},{1,0},{0,1},{0,-1}};
int a[maxn][maxn];
char G[maxn][maxn];
int r,c;
int bfs()
{
int jx,jy;
while (!q.empty())q.pop();
memset(a,-1,sizeof a);
REP(i,0,r-1){
REP(j,0,c-1){
switch(G[i][j]){
case '#': a[i][j]=-2;break;
case 'J': a[i][j]=-3;jx=i;jy=j;break;
case 'F': a[i][j]= 0;q.push(make_pair(i,j));break;
default: break;
}
}
}
while (!q.empty())
{
pair<int,int> temp=q.front();
q.pop();
int x=temp.first;
int y=temp.second;
REP(i,0,3){
int xt=x+moveto[i][0];
int yt=y+moveto[i][1];
if(xt<0 || yt<0 || xt>=r || yt>=c) continue;
if(a[xt][yt]!=-1) continue;
a[xt][yt]=a[x][y]+1;
q.push(make_pair(xt,yt));
}
}
int cnt=0;
q.push(make_pair(jx,jy));
while (!q.empty())
{
pair<int,int> temp=q.front();
q.pop();
int x=temp.first;
int y=temp.second;
cnt=-3-a[x][y];
if(!x || !y || x==r-1 || y==c-1) return cnt+1;
REP(i,0,3){
int xt=x+moveto[i][0];
int yt=y+moveto[i][1];
if(xt<0 || yt<0 || xt>=r || yt>=c) continue;
if(a[xt][yt]<0 && a[xt][yt]!=-1) continue;
if(a[xt][yt]>=0 && a[xt][yt]-1<=cnt) continue;
a[xt][yt]=a[x][y]-1;
q.push(make_pair(xt,yt));
}
}
return -1;
}
int main()
{
int n;
cin>>n;
while (n--)
{
scanf("%d%d",&r,&c);
REP(I,0,r-1)
scanf("%s",&G[I]);
int ans=bfs();
if(ans==-1) puts("IMPOSSIBLE");
else printf("%d\n",ans);
}
}
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <ctime>
#include <vector>
#include <cstring>
#include <map>
#include <string>
#include <queue>
using namespace std;
#define LL long long
#define ULL unsigned long long
//#define REP(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define INFLL (1LL)<<62
#define mset(a) memset(a,0,sizeof a)
#define FR(a) freopen(a,"r",stdin)
#define FW(a) freopen(a,"w",stdout)
#define PI 3.141592654
const LL MOD = 1000000007;
const int maxn = 1011;
queue<pair<int,int> > q;
int moveto[][2]={{-1,0},{1,0},{0,1},{0,-1}};
int a[maxn][maxn];
char G[maxn][maxn];
int r,c;
int bfs()
{
int jx,jy;
while (!q.empty())q.pop();
memset(a,-1,sizeof a);
REP(i,0,r-1){
REP(j,0,c-1){
switch(G[i][j]){
case '#': a[i][j]=-2;break;
case 'J': a[i][j]=-3;jx=i;jy=j;break;
case 'F': a[i][j]= 0;q.push(make_pair(i,j));break;
default: break;
}
}
}
while (!q.empty())
{
pair<int,int> temp=q.front();
q.pop();
int x=temp.first;
int y=temp.second;
REP(i,0,3){
int xt=x+moveto[i][0];
int yt=y+moveto[i][1];
if(xt<0 || yt<0 || xt>=r || yt>=c) continue;
if(a[xt][yt]!=-1) continue;
a[xt][yt]=a[x][y]+1;
q.push(make_pair(xt,yt));
}
}
int cnt=0;
q.push(make_pair(jx,jy));
while (!q.empty())
{
pair<int,int> temp=q.front();
q.pop();
int x=temp.first;
int y=temp.second;
cnt=-3-a[x][y];
if(!x || !y || x==r-1 || y==c-1) return cnt+1;
REP(i,0,3){
int xt=x+moveto[i][0];
int yt=y+moveto[i][1];
if(xt<0 || yt<0 || xt>=r || yt>=c) continue;
if(a[xt][yt]<0 && a[xt][yt]!=-1) continue;
if(a[xt][yt]>=0 && a[xt][yt]-1<=cnt) continue;
a[xt][yt]=a[x][y]-1;
q.push(make_pair(xt,yt));
}
}
return -1;
}
int main()
{
int n;
cin>>n;
while (n--)
{
scanf("%d%d",&r,&c);
REP(I,0,r-1)
scanf("%s",&G[I]);
int ans=bfs();
if(ans==-1) puts("IMPOSSIBLE");
else printf("%d\n",ans);
}
}