Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 36512 | Accepted: 13921 |
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!主要图的存储是一个三维的形式,读取和方向数组要注意,其他正常bfs
/*************************************************************************
> File Name: poj2251.cpp
> Author: YinJianxiang
> Mail: YinJianxiang123@gmail.com
> Created Time: 2017年07月26日 星期三 00时37分53秒
************************************************************************/
#include <iostream>
#include <queue>
#include <map>
#include <stack>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
int x,y,z;
typedef struct node {
int x;
int y;
int z;
int step;
} node;
node begin;
node end;
queue<node> que;
bool visited[35][35][35];
string s[35][35];
int dx[] = {1,-1,0,0,0,0};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {0,0,0,0,1,-1};
void clearQueue() {
while(!que.empty()) {
que.pop();
}
}
bool isSafe(int tx,int ty,int tz) {
if(tx >= 0 && tx < x && ty >= 0 && ty < y &&
tz >= 0 && tz < z && visited[tx][ty][tz] == true) {
return true;
}
return false;
}
int bfs() {
que.push(begin);
visited[begin.x][begin.y][begin.z] = false;
while(!que.empty()) {
node front = que.front();
//cout << front.x << front.y << front.z << endl;
que.pop();
if(front.x == end.x && front.y == end.y && front.z == end.z) {
//cout << "asdfg" << endl;
return front.step;
}
for(int i = 0;i < 6;i++) {
node next;
next.x = front.x + dx[i];
next.y = front.y + dy[i];
next.z = front.z + dz[i];
next.step = front.step + 1;
if(isSafe(next.x,next.y,next.z) == true) {
que.push(next);
visited[next.x][next.y][next.z] = false;
}
}
}
return -1;
}
int main() {
while(cin >> x >> y >> z) {
if(x == 0 && y == 0 && z == 0) {
break;
}
clearQueue();
memset(visited,true,sizeof(visited));
for(int i = 0;i < x;i++) {
for(int j = 0;j < y;j++) {
cin >> s[i][j];
for(int k = 0;k < z;k++) {
if(s[i][j][k] == '#') {
visited[i][j][k] = false;
}
if(s[i][j][k] == '.') {
visited[i][j][k] = true;
}
if (s[i][j][k] == 'S'){
begin.x = i;
begin.y = j;
begin.z = k;
begin.step = 0;
visited[i][j][k] = true;
}
else if (s[i][j][k] == 'E'){
end.x = i;
end.y = j;
end.z = k;
end.step = 0;
visited[i][j][k] = true;
//cout << end.x << end.y << end.z << "\n" << "\n";
}
}
}
}
int t = bfs();
if (t == -1) {
cout << "Trapped!" << "\n";
} else {
cout << "Escaped in " << t << " minute(s)." << "\n";
}
}
return 0;
}