Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14395 | Accepted: 5586 |
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.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
Source
Ulm Local 1997
题意:
题意:
主人公要逃出三维空间的地牢,‘S’是起始点,‘E‘是出口,’#‘是墙,’.‘是空格。思路:
移动方向可以是上,下,左,右,前,后。
问逃出迷宫的最短时间,若不能逃出输出“Trapped!”。
广度搜索题,只是将二维改变成了三维,本质上还是用广度搜索求最优解。
/*************************************************************************
> File Name: D.cpp
> Author: BSlin
> Mail:
> Created Time: 2013年09月27日 星期五 19时28分24秒
************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif
struct node {
int x,y,z,step;
}p1,p2;
int L,R,C,stx,sty,stz,enx,eny,enz;
bool flag;
char Map[40][40][40];
int to[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool inmap (int x, int y, int z) {
if(x >= 1 && x <= L && y >= 1 && y <= R && z >= 1 && z <= C)
return true;
return false;
}
int bfs(int x, int y, int z) {
int i;
queue<node>qu;
p1.x = x;
p1.y = y;
p1.z = z;
p1.step = 0;
qu.push(p1);
while(!qu.empty()) {
p1 = qu.front();
qu.pop();
if(p1.x == enx && p1.y == eny && p1.z == enz) return p1.step;
for(i=0; i<6; i++) {
p2.x = p1.x + to[i][0];
p2.y = p1.y + to[i][1];
p2.z = p1.z + to[i][2];
if(!inmap(p2.x,p2.y,p2.z) || Map[p2.x][p2.y][p2.z] == '#') continue;
Map[p2.x][p2.y][p2.z] = '#';
p2.step = p1.step + 1;
qu.push(p2);
}
}
return 0;
}
int main(int argc, char** argv) {
//read;
int i,j,k,ans;
while(scanf("%d %d %d\n", &L, &R, &C)) {
if(L == 0 && R == 0 && C == 0) break;
for(i=1; i<=L; i++) {
for(j=1; j<=R; j++) {
for(k=1; k<=C; k++) {
scanf("%c", &Map[i][j][k]);
if(Map[i][j][k] == 'S') {
stx = i;
sty = j;
stz = k;
}
else if(Map[i][j][k] == 'E') {
enx = i;
eny = j;
enz = k;
}
}
getchar();
}
getchar();
}
flag = 0;
ans = bfs(stx,sty,stz);
if(ans != 0) {
printf("Escaped in %d minute(s).\n",ans);
}
else printf("Trapped!\n");
}
return 0;
}