【代码超详解】POJ 2251 Dungeon Master(BFS 求最短路,0 ms)
一、题目描述
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 68551 Accepted: 24795
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?
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!
Source
Ulm Local 1997
二、算法分析说明与代码编写指导
三、AC 代码(0 ms)
#include<cstdio>
#include<queue>
#pragma warning(disable:4996)
using namespace std;
struct point { int x, y, z; };
queue<point> q; int L, R, C, d[32][32][32], M; point s, e, f, c;
int main() {
for (;;) {
scanf("%d%d%d", &L, &R, &C); if (L == 0)return 0;
getchar(); M = 0;
while (q.empty() == false)q.pop();
for (int k = 1; k <= L + 1; ++k)
for (int i = 1; i <= R + 1; ++i)
for (int j = 1; j <= C + 1; ++j)d[i][j][k] = 0;
for (int k = 1; k <= L; ++k) {
for (int i = 1; i <= R; ++i) {
for (int j = 1; j <= C; ++j) {
d[i][j][k] = getchar();
if (d[i][j][k] == 'S') { d[i][j][k] = 0; s.x = i; s.y = j; s.z = k; }
if (d[i][j][k] == 'E') { d[i][j][k] = '.'; e.x = i; e.y = j; e.z = k; }
}
getchar();
}
getchar();
}
q.push(s);
while (q.empty() == false) {
f = q.front(); M = d[f.x][f.y][f.z] + 1;
if (d[f.x + 1][f.y][f.z] == '.') { c.x = f.x + 1; c.y = f.y; c.z = f.z; q.push(c); d[c.x][c.y][c.z] = M; }
if (d[f.x - 1][f.y][f.z] == '.') { c.x = f.x - 1; c.y = f.y; c.z = f.z; q.push(c); d[c.x][c.y][c.z] = M; }
if (d[f.x][f.y + 1][f.z] == '.') { c.x = f.x; c.y = f.y + 1; c.z = f.z; q.push(c); d[c.x][c.y][c.z] = M; }
if (d[f.x][f.y - 1][f.z] == '.') { c.x = f.x; c.y = f.y - 1; c.z = f.z; q.push(c); d[c.x][c.y][c.z] = M; }
if (d[f.x][f.y][f.z + 1] == '.') { c.x = f.x; c.y = f.y; c.z = f.z + 1; q.push(c); d[c.x][c.y][c.z] = M; }
if (d[f.x][f.y][f.z - 1] == '.') { c.x = f.x; c.y = f.y; c.z = f.z - 1; q.push(c); d[c.x][c.y][c.z] = M; }
q.pop();
}
if (d[e.x][e.y][e.z] != '.')printf("Escaped in %d minute(s).\n", d[e.x][e.y][e.z]);
else puts("Trapped!");
}
}
下一篇: Java中二分法查找算法的深入理解