标签:
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!
1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 6 struct point 7 { 8 int x,y,z; 9 int step; 10 }; 11 12 int sx,sy,sz,ex,ey,ez; 13 int L,R,C; 14 char map[32][32][32]; 15 int vis[32][32][32]; 16 int d[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,0,1},{0,-1,0},{0,0,-1}}; 17 18 bool check(point node) 19 { 20 if(node.x==ex&&node.y==ey&&node.z==ez) 21 return true; 22 return false; 23 } 24 25 int bfs(int z,int x,int y) 26 { 27 queue<point>q; 28 point init; 29 init.x=x,init.y=y,init.z=z; 30 init.step=0; 31 q.push(init); 32 vis[z][x][y]=1; 33 while(!q.empty()) 34 { 35 point t1,t2; 36 t1=q.front(); 37 q.pop(); 38 if(check(t1)) 39 return t1.step; 40 for(int i=0;i<6;i++) 41 { 42 t2.z=t1.z+d[i][0]; 43 t2.x=t1.x+d[i][1]; 44 t2.y=t1.y+d[i][2]; 45 t2.step=t1.step+1; 46 if(!vis[t2.z][t2.x][t2.y]&&map[t2.z][t2.x][t2.y]!=‘#‘) 47 { 48 q.push(t2); 49 vis[t2.z][t2.x][t2.y]=1; 50 } 51 } 52 } 53 return -1; 54 } 55 56 int main() 57 { 58 //freopen("in.txt","r",stdin); 59 char temp; 60 int i,j,k; 61 while(scanf("%d%d%d",&L,&R,&C),L||R||C) 62 { 63 memset(map,‘#‘,sizeof(map)); 64 memset(vis,0,sizeof(vis)); 65 for(i=1;i<=L;i++) 66 { 67 for(j=1;j<=R;j++) 68 { 69 getchar(); 70 for(k=1;k<=C;k++) 71 { 72 scanf("%c",&temp); 73 map[i][j][k]=temp; 74 if(temp==‘S‘) 75 sz=i,sx=j,sy=k; 76 else if(temp==‘E‘) 77 ez=i,ex=j,ey=k; 78 else; 79 } 80 } 81 getchar(); 82 } 83 int ans=bfs(sz,sx,sy); 84 if(ans==-1) 85 printf("Trapped!\n"); 86 else 87 printf("Escaped in %d minute(s).\n",ans); 88 } 89 return 0; 90 }
标签:
原文地址:http://www.cnblogs.com/homura/p/4705890.html