标签:
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!
ps:和最基础的迷宫问题一样,只是2维变3维
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 int l,r,c,ans; 8 char a[222][222][222]; 9 bool b[222][222][222]; 10 int nx[6]={-1,1,0,0,0,0}; 11 int ny[6]={0,0,-1,1,0,0}; 12 int nz[6]={0,0,0,0,-1,1}; 13 struct node{ 14 int x; 15 int y; 16 int z; 17 int step; 18 }; 19 int BFS(int z,int x,int y) 20 { 21 node s; 22 s.x=x; 23 s.y=y; 24 s.z=z; 25 s.step=0; 26 b[z][x][y]=true; 27 queue<node>q; 28 q.push(s); 29 while(!q.empty()) 30 { 31 node now=q.front(); 32 q.pop(); 33 if(a[now.z][now.x][now.y]==‘E‘) 34 return now.step; 35 for(int i = 0;i < 6;i++) 36 { 37 node end; 38 end.x=now.x+nx[i]; 39 end.y=now.y+ny[i]; 40 end.z=now.z+nz[i]; 41 end.step=now.step; 42 if(b[end.z][end.x][end.y]==false&&(a[end.z][end.x][end.y]==‘.‘||a[end.z][end.x][end.y]==‘E‘)) 43 { 44 b[end.z][end.x][end.y]=true; 45 end.step+=1; 46 q.push(end); 47 } 48 49 } 50 } 51 return -1; 52 53 } 54 int main() 55 { 56 while(scanf("%d %d %d",&l,&r,&c),l+r+c) 57 { 58 int i,j,k; 59 memset(b,false,sizeof(b)); 60 memset(a,‘#‘,sizeof(a)); 61 for(i = 0;i < l;i++) 62 { 63 for(j = 0;j < r;j++) 64 scanf("%s",a[i][j]); 65 } 66 for(i = 0;i < l;i++) 67 for(j = 0;j < r;j++) 68 for(k = 0;k <c;k++) 69 { 70 if(a[i][j][k]==‘S‘) 71 { 72 ans=BFS(i,j,k); 73 break; 74 } 75 } 76 if(ans == -1) 77 printf("Trapped!\n"); 78 else 79 printf("Escaped in %d minute(s).\n",ans); 80 81 82 } 83 return 0; 84 } //这个题应该可以优化!。
标签:
原文地址:http://www.cnblogs.com/llal/p/5719646.html