1. 程式人生 > >[POJ 2251] Dungeon Master

[POJ 2251] Dungeon Master

題目連結:http://poj.org/problem?id=2251

注意:細心細心再細心!!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 
 7 const int maxn = 35;
 8 int L,R,C;
 9 bool vis[maxn][maxn][maxn];
10 char maze[maxn][maxn][maxn];
11 int go[6][3] = {0
,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0}; 12 struct node 13 { 14 int x,y,z; 15 int count; 16 }now,nex; 17 18 bool IsOk(node s) 19 { 20 return (s.x>=0&&s.x<L&&s.y>=0&&s.y<R&&s.z>=0&&s.z<C&&!vis[s.x][s.y][s.z]&&maze[s.x][s.y][s.z]!='
#'); 21 } 22 23 int bfs() 24 { 25 queue<node> Q; 26 vis[now.x][now.y][now.z]=1; 27 Q.push(now); 28 while(!Q.empty()) 29 { 30 now = Q.front(); 31 Q.pop(); 32 if(maze[now.x][now.y][now.z]=='E') 33 return now.count; 34 for(int i=0;i<6
;i++) 35 { 36 nex.x = now.x + go[i][0]; 37 nex.y = now.y + go[i][1]; 38 nex.z = now.z + go[i][2]; 39 if(IsOk(nex)) 40 { 41 vis[nex.x][nex.y][nex.z]=1; 42 nex.count = now.count + 1; 43 Q.push(nex); 44 } 45 } 46 } 47 return 0; 48 } 49 50 int main() 51 { 52 while(~scanf("%d%d%d",&L,&R,&C)&&(L||R||C)) 53 { 54 for(int i=0;i<L;i++) 55 { 56 for(int j=0;j<R;j++) 57 scanf("%s",maze[i][j]); 58 getchar(); 59 } 60 int flag = 0; 61 for(int i=0;i<L;i++) 62 { 63 for(int j=0;j<R;j++) 64 { 65 for(int k=0;k<C;k++) 66 if(maze[i][j][k]=='S') 67 { 68 now.x=i;now.y=j;now.z=k; 69 now.count=0; 70 flag = 1; 71 break; 72 } 73 if(flag) 74 break; 75 } 76 if(flag) 77 break; 78 } 79 memset(vis,0,sizeof(vis)); 80 int ans = bfs(); 81 if(ans) 82 printf("Escaped in %d minute(s).\n",ans); 83 else 84 printf("Trapped!\n"); 85 } 86 return 0; 87 }