Dungeon Master
Dungeon Master |
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 Specification
The input file 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 Specification
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!
多层迷宫题...
只需设一个三维数组即可..但是要注意起点不一定是在 (0,0,0)的位置!!!
CODE:
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char map[35][35][35];
int vis[35][35][35];
int ans[35][35][35];
struct node
{
int nn,rr,cc;
node(int nn=0,int rr=0,int cc=0):nn(nn),rr(rr),cc(cc){};
};
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,-1,1,0,0};
int dz[]={0,0,0,0,1,-1};
int n,r,c;
int bfs(int z,int x,int y)
{
node now,next;
now=node(z,x,y);
queue<node>que;
que.push(now);
ans[z][x][y]=0;
memset(vis,0,sizeof(vis));
vis[z][x][y]=1;
while(!que.empty())
{
now=que.front();
que.pop();
if(map[now.nn][now.rr][now.cc]=='E')
{
return ans[now.nn][now.rr][now.cc];
}
for(int i=0;i<6;i++)
{
int nz=now.nn+dz[i];
int nx=now.rr+dx[i];
int ny=now.cc+dy[i];
next=node(nz,nx,ny);
if(nz>=0&&nz<n&&nx>=0&&nx<r&&ny>=0&&ny<c&&map[nz][nx][ny]!='#'&&vis[nz][nx][ny]==0)
{
ans[nz][nx][ny]=ans[now.nn][now.rr][now.cc]+1;
vis[nz][nx][ny]=1;
que.push(next);
}
}
}
return 0;
}
int main()
{
//freopen("in.in","r",stdin);
while(~scanf("%d%d%d",&n,&r,&c),n,r,c)
{
for(int j=0;j<n;j++)
{
for(int i=0;i<r;i++)
{
scanf("%s",map[j][i]);
}
getchar();
}
int ok=0;
int ii,jj,kk;
for(int i=0;i<n;i++)
{
for(int j=0;j<r;j++)
{
for(int k=0;k<c;k++)
{
if(map[i][j][k]=='S')
{
ok=1;
ii=i;jj=j;kk=k;
break;
}
}
if(ok) break;
}
if(ok) break;
}
int s=bfs(ii,jj,kk);
if(s==0) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",s);
}
return 0;
}