UVA - 11624 J - Fire! (BFS)


J – 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.


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.


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






3 3




Sample Output






#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue>
using namespace std; typedef long long ll; typedef unsigned long long ull; #define mod 1000000007
#define INF 0x3f3f3f3f
#define MAX 1005
int n,m; int sx,sy,fx,fy; char g[MAX][MAX]; bool vis[MAX][MAX]; int fire[MAX][MAX]; int ans=INF; int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; struct mask { int x,y,step; mask(){} mask(int xx,int yy,int st) { x=xx,y=yy,step=st; } }; struct fir { int x,y,time; fir(){} fir(int xx,int yy,int ti) { x=xx,y=yy,time=ti; } }; queue<mask>q; queue<fir>fi; bool check(int a,int b) { return 0<=a&&a<n&&0<=b&&b<m&&g[a][b]!='#'; } //遍历火的蔓延速度
void bfs_fire() { while(fi.size()) { fir tmp=fi.front();fi.pop(); for(int i=0;i<4;i++) { int nx=tmp.x+dx[i]; int ny=tmp.y+dy[i]; if(fire[nx][ny]>tmp.time+1&&check(nx,ny)) {//cout<<"ok"<<endl;
                fire[nx][ny]=min(fire[nx][ny],tmp.time+1); fi.push(fir(nx,ny,tmp.time+1)); } } } } //遍历人的可行路径
int bfs() { memset(vis,false,sizeof(vis)); while(q.size())q.pop(); vis[sx][sy]=true; q.push(mask(sx,sy,0)); while(q.size()) { mask tmp=q.front();q.pop(); if(tmp.x==n-1||tmp.y==m-1||tmp.x==0||tmp.y==0) { ans=min(ans,tmp.step); } for(int i=0;i<4;i++) { int nx=tmp.x+dx[i]; int ny=tmp.y+dy[i]; if(check(nx,ny)&&tmp.step+1<fire[nx][ny]&&!vis[nx][ny]) { vis[nx][ny]=true; q.push(mask(nx,ny,tmp.step+1)); } } } return ans==INF?-1:ans; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); while(fi.size())fi.pop(); memset(fire,INF,sizeof(fire)); for(int i=0;i<n;i++) { scanf("%s",&g[i]); for(int j=0;j<m;j++) { if(g[i][j]=='J') { sx=i,sy=j; } if(g[i][j]=='F') { fire[i][j]=0;//注意这里的火可能不止一个,所以要全部加入
                     fi.push(fir(i,j,0)); } } } ans=INF; bfs_fire(); /* for(int i=0;i<n;i++){ for(int j=0;j<m;j++) cout<<fire[i][j]<<" "; cout<<endl; }*/
         int d=bfs(); if(d==-1) printf("IMPOSSIBLE\n"); else printf("%d\n",d+1); } return 0; }


    原文地址: https://www.cnblogs.com/zhgyki/p/9508782.html