Description
Input
Output
Escaped in x minute(s).where x is replaced by the shortest time it takes to escape. If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
题意:3维迷宫!“.”是路;“#”是墙,“E"进“S”出
这题的题目略有小坑;
注意几个地方:1.数组开到105以上;2.队列清空;
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<queue> 6 using namespace std; 7 int s[133][133][133]; 8 int vis[133][133][133]; 9 int r[6]= {-1,1,0,0,0,0}; 10 int c[6]= {0,0,-1,1,0,0}; 11 int h[6]= {0,0,0,0,1,-1}; 12 int ei,ej,ek; 13 struct node 14 { 15 int a,b,c; 16 int step; 17 }; 18 node p,q; 19 void getit(int I,int J,int K)//输入并处理 20 { 21 char ch; 22 int i,j,k; 23 for(k=1; k<K; k++) 24 { 25 for(i=1; i<I; i++) 26 { 27 for(j=1; j<J; j++) 28 { 29 scanf("%c",&ch); 30 if(ch=='.') 31 s[i][j][k]=-1; 32 else if(ch=='E') 33 { 34 ei=i; 35 ej=j; 36 ek=k; 37 } 38 else if(ch=='S') 39 s[i][j][k]=1; 40 } 41 getchar(); 42 } 43 getchar(); 44 } 45 } 46 void bfs(int ai,int aj, int ak,int step)//简单BFS 47 { 48 queue<node>que; 49 vis[ai][aj][ak]=1; 50 int t; 51 p.a=ai; 52 p.b=aj; 53 p.c=ak; 54 p.step=step; 55 que.push(p); 56 while(!que.empty()) 57 { 58 p=que.front(); 59 que.pop(); 60 for(t=0; t<6; t++) 61 { 62 q.a=p.a+r[t]; 63 q.b=p.b+c[t]; 64 q.c=p.c+h[t]; 65 q.step=p.step+1; 66 if(s[q.a][q.b][q.c]&&!vis[q.a][q.b][q.c]) 67 { 68 if(s[q.a][q.b][q.c]==1) 69 { 70 printf("Escaped in %d minute(s).\n",q.step); 71 return ; 72 } 73 que.push(q); 74 vis[q.a][q.b][q.c]=1; 75 } 76 } 77 } 78 printf("Trapped!\n"); 79 } 80 int main() 81 { 82 83 int I,J,K; 84 while(scanf("%d %d %d",&K,&I,&J)&&(I||J||K)) 85 { 86 getchar(); 87 memset(s,0,sizeof(s));//置空 88 memset(vis,0,sizeof(vis)); 89 getit(I+1,J+1,K+1); 90 bfs(ei,ej,ek,0); 91 } 92 return 0; 93 }