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!
一个3D迷宫,入口为S,出口为E,可以上下东西南北移动,每移动 一格,花费一秒,问从S~E最短时间;;
简单搜索,每次判断6个方向是否走,用数组d[][][]记录从S到d[i][j][k]的最短路径,输出d[Ei][Ej][Ek]就行了;;
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; const int inf=0x3f3f3f3f; char str[35][35][35]; int dx[6]={0,1,0,-1,0,0}; int dy[6]={1,0,-1,0,0,0}; int dz[6]={0,0,0,0,-1,1}; int n,m,z; int A,B,C; int d[35][35][35]; bool f(int lz,int lx,int ly) { if(lz>=0&&lz<z&&lx>=0&&lx<n&&ly>=0&&ly<m) return true; return false; } int dfs(int lz,int lx,int ly) { int nx,ny,nz; for(int i=0;i<6;i++) { nx=lx+dx[i]; ny=ly+dy[i]; nz=lz+dz[i]; if(f(nz,nx,ny)) { if(str[nz][nx][ny]!='#'&&d[nz][nx][ny]==inf) { d[nz][nx][ny]=d[lz][lx][ly]+1; dfs(nz,nx,ny); } } } return d[A][B][C]; } int main() { int a,b,c; while(~scanf("%d%d%d",&z,&n,&m)) { if(z==0&&n==0&&m==0) break; for(int i=0;i<z;i++) { for(int j=0;j<n;j++) { scanf("%s",str[i][j]); for(int k=0;k<m;k++) { d[i][j][k]=inf; if(str[i][j][k]=='S') a=i,b=j,c=k; if(str[i][j][k]=='E') A=i,B=j,C=k; } } } d[a][b][c]=0; int ans=dfs(a,b,c); if(ans==inf) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } //cout << "Hello world!" << endl; return 0; }