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?
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.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
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!
// 简单广搜,只不过变成三维,因此在方向上,可以移动6个方向,其他的很简单,存储时直接用三维数组存就行
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int l,r,c,sx,sy,sz,ex,ey,ez;
struct point
{
int x,y,z;
int step;
};
int dis[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
char a[50][50][50];
int visited[50][50][50];
int bfs(int startx,int starty,int startz,int endx,int endy,int endz)
{
point news,now;
queue<point> t;
now.x=startx;
now.y=starty;
now.z=startz;
now.step=0;
visited[now.x][now.y][now.z]=1;
t.push(now);
while(!t.empty())
{
now=t.front();
t.pop();
if(now.x==ex&&now.y==ey&&now.z==ez) return now.step;
for(int i = 0;i < 6;i++)
{
news.x=now.x+dis[i][0];
news.y=now.y+dis[i][1];
news.z=now.z+dis[i][2];//六个方向;
if(news.x>=l||news.x<0||news.y<0||news.y>=r||news.z<0||news.z>=c) continue;//出界
if(a[news.x][news.y][news.z]=='#'||visited[news.x][news.y][news.z]==1) continue;//已经走过或者该位置不能走
visited[news.x][news.y][news.z]=1;
news.step=now.step+1;
t.push(news);
}
}
return 0;
}
int main() {
while(cin>>l>>r>>c&&l!=0&&r!=0&&c!=0)
{
for(int i = 0;i < l; i++){
for(int j = 0;j < r;j++){
for(int k = 0;k < c;k++){
cin >> a[i][j][k];
if(a[i][j][k] == 'S')//记录开始位置
{
sx=i;
sy=j;
sz=k;
}
if(a[i][j][k]=='E')//记录结束位置
{
ex=i;
ey=j;
ez=k;
}
}
}
}
int b=bfs(sx,sy,sz,ex,ey,ez);
memset(visited,0,sizeof(visited));
if(b==0) cout<<"Trapped!"<<endl;
else cout<<"Escaped in "<<b<<" minute(s)."<<endl;
}
return 0;
}