标签:
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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<cmath> 6 #include<stdlib.h> 7 using namespace std; 8 int k,n,m; 9 char map[36][36][36]; 10 int vis[36][36][36]; 11 struct Node 12 { 13 int floor; 14 int x,y; 15 int t; 16 }st,ed; 17 int dirx[]={0,0,-1,1}; 18 int diry[]={-1,1,0,0}; 19 void bfs(Node s) 20 { 21 queue<Node>q; 22 q.push(s); 23 vis[s.floor][s.x][s.y]=1; 24 Node t1,t2; 25 while(!q.empty()) 26 { 27 t1=q.front(); 28 q.pop(); 29 if(t1.floor==ed.floor && t1.x==ed.x && t1.y==ed.y) 30 { 31 printf("Escaped in %d minute(s).\n",t1.t); 32 return; 33 } 34 35 for(int i=0;i<4;i++) 36 { 37 t2.floor=t1.floor; 38 t2.x=t1.x+dirx[i]; 39 t2.y=t1.y+diry[i]; 40 if(t2.x>=0 && t2.x<n && t2.y>=0 && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!=‘#‘) 41 { 42 vis[t2.floor][t2.x][t2.y]=1; 43 t2.t=t1.t+1; 44 q.push(t2); 45 } 46 } 47 t2=t1; 48 t2.floor=t1.floor+1; 49 if(t2.floor<0 || t2.floor>=k) continue; 50 if(t2.x>=0 && t2.x<n && t2.y>=0 && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!=‘#‘) 51 { 52 vis[t2.floor][t2.x][t2.y]=1; 53 t2.t=t1.t+1; 54 q.push(t2); 55 } 56 57 t2=t1; 58 t2.floor=t1.floor-1; 59 if(t2.floor<0 || t2.floor>=k) continue; 60 if(t2.x>=0 && t2.x<n && t2.y>=0 && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!=‘#‘) 61 { 62 vis[t2.floor][t2.x][t2.y]=1; 63 t2.t=t1.t+1; 64 q.push(t2); 65 } 66 } 67 printf("Trapped!\n"); 68 } 69 int main() 70 { 71 while(scanf("%d%d%d",&k,&n,&m)==3 && n+m+k!=0) 72 { 73 for(int i=0;i<k;i++) 74 { 75 for(int j=0;j<n;j++) 76 { 77 scanf("%s",map[i][j]); 78 for(int l=0;l<m;l++) 79 { 80 if(map[i][j][l]==‘S‘) 81 { 82 st.floor=i; 83 st.x=j; 84 st.y=l; 85 st.t=0; 86 } 87 if(map[i][j][l]==‘E‘) 88 { 89 ed.floor=i; 90 ed.x=j; 91 ed.y=l; 92 } 93 } 94 } 95 } 96 memset(vis,0,sizeof(vis)); 97 bfs(st); 98 } 99 return 0; 100 }
标签:
原文地址:http://www.cnblogs.com/UniqueColor/p/4732898.html