Dungeon Master
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
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 } //这个题应该可以优化!。