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.
2 4 4 #### #JF# #..# #..# 3 3 ### #J. #.F
3 IMPOSSIBLEMalcolm Sharpe, Ondřej Lhoták
思路:入门级BFS,加了fire,其实还是一样,只要预处理把所有fire点入队,然后再把起始点入队,进行BFS,搜到边界就有解。
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int mm=1000+9; const int dx[]={-1,0,1,0}; const int dy[]={0,-1,0,1}; bool vis[mm][mm]; class node { public:int x,y,fire,step; }; char g[mm][mm]; int n,m,cas; node s,t; queue<node>q; void get(int n,int m) {while(!q.empty())q.pop(); memset(vis,0,sizeof(vis)); char c; for(int i=0;i<n;++i) for(int j=0;j<m;++j) { while(1) {c=getchar(); switch(c) { case '.':g[i][j]=c;goto there; case '#':g[i][j]=c;goto there; case 'J':s.x=i;s.y=j;s.step=0;s.fire=0;g[i][j]=c;vis[i][j]=1;goto there; case 'F':t.x=i;t.y=j;t.fire=1;t.step=0;q.push(t);g[i][j]=c;vis[i][j]=1;goto there; } } there:; } q.push(s); } int bfs() { node z; while(!q.empty()) { z=q.front();q.pop(); for(int i=0;i<4;++i) { t=z;t.x+=dx[i];t.y+=dy[i];t.step++; if(z.fire) { if(t.x<0||t.x>=n||t.y<0||t.y>=m||vis[t.x][t.y]||g[t.x][t.y]=='#')continue; q.push(t);vis[t.x][t.y]=1; } else { if(t.x<0||t.x>=n||t.y<0||t.y>=m)return t.step; if(vis[t.x][t.y]||g[t.x][t.y]=='#')continue; q.push(t);vis[t.x][t.y]=1; } } } return -1; } int main() { while(~scanf("%d",&cas)) { while(cas--) { scanf("%d%d",&n,&m); get(n,m); int ans=bfs(); if(ans<0)printf("IMPOSSIBLE\n"); else printf("%d\n",ans); } } return 0; }