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?
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.
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!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
一定要记得清空vis。。。。(忘了清空wa了1次)
思路很简单,三维迷宫创建一个三维数组就可以了,移动方向再把上下移动加进去。
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string.h> #include <queue> using namespace std; const int maxn = 50; char s[maxn][maxn][maxn]; int l,r,c; int sx,sy,sk; int ex,ey,ek; int dx[]={1,-1,0,0,0,0}; int dy[]={0,0,1,-1,0,0}; int dk[]={0,0,0,0,1,-1}; int vis[maxn][maxn][maxn]; struct point { int x,y,k; }; bool judge(int x,int y,int k) { return (x>=1&&y>=1&&k>=1&&x<=l&&y<=r&&k<=c&&vis[x][y][k]==0&&s[x][y][k]!='#'); } int bfs() { queue<point>que; point t ; t.x=sx;t.y=sy;t.k=sk; que.push(t); while(que.size()) { point p=que.front();que.pop(); if(p.x==ex&&p.y==ey&&p.k==ek) return vis[ex][ey][ek]; for(int i=0;i<6;i++) { int nx=p.x+dx[i]; int ny=p.y+dy[i]; int nk=p.k+dk[i]; if(judge(nx,ny,nk)) { vis[nx][ny][nk]=vis[p.x][p.y][p.k]+1; t.x=nx;t.y=ny;t.k=nk; que.push(t); } // if(vis[ex][ey][ek]) break; } // if(vis[ex][ey][ek]) break; } return vis[ex][ey][ek]; } int main() { while(scanf("%d%d%d",&l,&r,&c)) { memset(vis,0,sizeof(vis)); memset(s,0,sizeof(s)); if(l==0&&r==0&&c==0) break; for(int i=1;i<=l;i++) { for(int j=1;j<=r;j++) { scanf("%s",&s[i][j][1]); for(int k=1;k<=c;k++) { if(s[i][j][k]=='S') { sx=i;sy=j;sk=k; // cout << i << ' ' << j << " "<<k <<endl; } else if(s[i][j][k]=='E') { ex=i;ey=j;ek=k; } } } } // for(int i=1;i<=l;i++) // { // for(int j=1;j<=r;j++) // { // for(int k=1;k<=c;k++) // { // cout << s[i][j][k]; // } // cout <<endl; // } // } int ans= bfs(); if(ans==0) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } return 0; }