Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17465 | Accepted: 6792 |
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
Source
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stdlib.h> #include<queue> #include<stack> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=30+5; const int INF=0x3f3f3f3f; const double EPS=1e-9; int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}}; int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; int f,n,m,vis[MAXN][MAXN][MAXN],ans; char map[MAXN][MAXN][MAXN]; struct node { int z,x,y; int step; bool operator<(const node A)const { return step>A.step; } }star,en; bool judge(int x,int y,int z) { if(0<=x && x<n && 0<=y && y<m && 0<=z && z<f && !vis[z][x][y] && map[z][x][y]!='#') return true; return false; } int BFS() { memset(vis,0,sizeof(vis)); priority_queue<node> Q; Q.push(star); vis[star.z][star.x][star.y]=1; while(!Q.empty()) { node now,next; now=Q.top(); Q.pop(); //printf("(%d %d %d) %d\n",now.z,now.x,now.y,now.step); if(now.z==en.z && now.x==en.x && now.y==en.y) return now.step; for(int i=0;i<4;i++) { next.x=now.x+dir4[i][0]; next.y=now.y+dir4[i][1]; next.z=now.z; if(judge(next.x,next.y,next.z)) { vis[next.z][next.x][next.y]=1; next.step=now.step+1; Q.push(next); } } next.z=now.z+1; next.x=now.x; next.y=now.y; if(judge(next.x,next.y,next.z)) { vis[next.z][next.x][next.y]=1; next.step=now.step+1; Q.push(next); } next.z=now.z-1; next.x=now.x; next.y=now.y; if(judge(next.x,next.y,next.z)) { vis[next.z][next.x][next.y]=1; next.step=now.step+1; Q.push(next); } } return INF; } int main() { while(scanf("%d %d %d",&f,&n,&m) && (f||n||m)) { memset(map,0,sizeof(map)); for(int i=0;i<f;i++) for(int j=0;j<n;j++) { cin>>map[i][j]; for(int k=0;k<m;k++) { if(map[i][j][k]=='S') { star.z=i; star.x=j; star.y=k; star.step=0; } if(map[i][j][k]=='E') { en.z=i; en.x=j; en.y=k; } } } ans=BFS(); if(ans==INF) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } return 0; }