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!
水题
题目大意:
一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped!
#include<cstdio> #include<cstring> #include<iostream> #include<queue> using namespace std; #define inf 0x3f3f3f3f int r,l,c,sx,sy,sz,t; char g[40][40][40]; bool vis[40][40][40]; int dx[6]={1,-1,0,0,0,0}; int dy[6]={0,0,0,0,-1,1}; int dz[6]={0,0,-1,1,0,0}; struct node { int x,y,z,st; }; int bfs() { int i,j,k; node a,b,h; queue<node> q; vis[sx][sy][sz]=1; a.x=sx,a.y=sy,a.z=sz,a.st=0; q.push(a); while(!q.empty()) { b=q.front(); q.pop(); if(g[b.x][b.y][b.z]=='E') return b.st; for(i=0;i<6;i++) { int nx=b.x+dx[i],ny=b.y+dy[i],nz=b.z+dz[i]; if(nx>=0&&nx<l&&ny>=0&&ny<r&&nz>=0&&nz<c&&!vis[nx][ny][nz]&&(g[nx][ny][nz]=='.'||g[nx][ny][nz]=='E')) { h.x=nx,h.y=ny,h.z=nz,h.st=b.st+1; vis[nx][ny][nz]=1; q.push(h); } } } return inf;//一定不要忘了啊啊啊 } int main() { int i,j,k; while(cin>>l>>r>>c,l||r||c) { memset(vis,0,sizeof vis); for(i=0;i<l;i++,getchar()) for(j=0;j<r;j++) { cin>>g[i][j]; for(k=0;k<c;k++) if(g[i][j][k]=='S') sx=i,sy=j,sz=k; } t=bfs(); if(t==inf) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",t); } return 0; }