标签:uva 11624 fire bfs 搜索
Time Limit: 1000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
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 IMPOSSIBLE
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<cstdlib> #include<set> #include<queue> #include<stack> #include<vector> #include<map> #define N 100010 #define Mod 10000007 #define lson l,mid,idx<<1 #define rson mid+1,r,idx<<1|1 #define lc idx<<1 #define rc idx<<1|1 const double EPS = 1e-11; const double PI = acos ( -1.0 ); const double E = 2.718281828; typedef long long ll; const int INF = 1000010; using namespace std; int n,m; bool vis[1010][1010]; char mp[1010][1010]; int xx[4]= {-1,0,1,0}; int yy[4]= {0,1,0,-1}; int Fx,Fy,Jx,Jy; int flag; int len; struct node { int x,y; int num; int fire; }; node q[10000]; queue<node>que; void bfs() { while(que.size()) que.pop(); memset(vis,0,sizeof vis); node a,t; for(int i=0; i<len; i++) { que.push(q[i]); vis[q[i].x][q[i].y]=1; } a.x=Jx,a.y=Jy,a.num=0,a.fire=0; que.push(a); vis[Jx][Jy]=1; while(que.size()) { a=que.front(); que.pop(); if((a.x==1||a.x==n||a.y==1||a.y==m)&&a.fire==0) { cout<<a.num+1<<endl; flag=0; return; } for(int i=0; i<4; i++) { t.x=xx[i]+a.x; t.y=yy[i]+a.y; if(t.x>=1&&t.x<=n&&t.y>=1&&t.y<=m&&!vis[t.x][t.y]&&mp[t.x][t.y]!='#') { t.num=a.num+1; t.fire=a.fire; que.push(t); vis[t.x][t.y]=1; } } } } int main() { int T; cin>>T; { while(T--) { scanf("%d%d",&n,&m); getchar(); len=0; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { scanf("%c",&mp[i][j]); if(mp[i][j]=='J') { Jx=i,Jy=j; } if(mp[i][j]=='F') { q[len].x=i,q[len].y=j,q[len].num=0,q[len++].fire=1; } } getchar(); } flag=1; bfs(); if(flag) cout<<"IMPOSSIBLE\n"; } } return 0; }
标签:uva 11624 fire bfs 搜索
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/42365133