Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18161 | Accepted: 7056 |
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 <queue>
#include <iostream>
#include <algorithm>
#define maxn 100
#define inf 0x3f3f3f3f
using namespace std;
char map[maxn][maxn][maxn],vis[maxn][maxn][maxn];
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
int sx,sy,sz,sum,A,B,C;
struct node {
int x,y,z,t;
};
bool check(node m){
if(m.x<0||m.y<0||m.z<0||m.x>=A||m.y>=B||m.z>=C)
return 0;
return 1;
}
void bfs(){
memset(vis,0,sizeof(vis));
node st,ed;
st.x=sx;
st.y=sy;
st.z=sz;
st.t=0;
queue<node>q;
vis[sx][sy][sz]=1;
q.push(st);
sum=inf;
while(!q.empty()){
st=q.front();
q.pop();
for(int i=0;i<6;++i){
ed=st;
ed.x+=dir[i][0];
ed.y+=dir[i][1];
ed.z+=dir[i][2];
ed.t++;
if(check(ed) && map[ed.x][ed.y][ed.z]!='#' && !vis[ed.x][ed.y][ed.z]){
if(map[ed.x][ed.y][ed.z]=='E'){
if(ed.t<sum)
sum=ed.t;
continue;
}
q.push(ed);
vis[ed.x][ed.y][ed.z]=1;
}
}
}
return ;
}
int main (){
while(scanf("%d%d%d",&A,&B,&C),A,B,C){
int i,j,k;
for(i=0;i<A;++i)
for(j=0;j<B;++j){
scanf("%s",map[i][j]);
for(k=0;k<C;++k){
if(map[i][j][k]=='S') {sx=i; sy=j; sz=k;};
}
}
bfs();
if(sum==inf) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",sum);
}
return 0;
}