POJ 2251 Dungeon Master

Posted kindleheart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2251 Dungeon Master相关的知识,希望对你有一定的参考价值。

POJ 2251 Dungeon Master

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!
分析:题目大意为求得一个3D立体迷宫中从S(起点)走向E(终点)的最短路径长度,BFS,用一个d[][][]三维数组记录长度输出即可
代码:
#include<iostream>
#include<cstdio> 
#include<cstring>
#include<queue>
using namespace std;
typedef struct {
    int x, y, z;
}loc;
char a[31][31][31];
int d[31][31][31];
int dx[6] = {0, 0, 0, 0, 1, -1};
int dy[6] = {1, -1, 0, 0, 0, 0};
int dz[6] = {0, 0, 1, -1, 0, 0};
int sx, sy, sz, ex, ey, ez;
int l, r, c;
int ans;
int bfs() {
    queue<loc> q;
    loc lo;
    lo.x = sx; lo.y = sy; lo.z = sz;
    q.push(lo);
    while(q.size()) {
        loc p = q.front(); q.pop();
        for(int i = 0; i < 6; i++) {
            int nx = p.x + dx[i];
            int ny = p.y + dy[i];
            int nz = p.z + dz[i];
            if(nx >= 0 && nx < r && ny >= 0 && ny < c && nz >= 0 && nz < l && a[nz][nx][ny] != #) {
                d[nz][nx][ny] = d[p.z][p.x][p.y] + 1;
                if(nx == ex && ny == ey && nz == ez) return ans = d[ez][ex][ey];
                a[nz][nx][ny] = #;
                lo.x = nx; lo.y = ny; lo.z = nz;
                q.push(lo);
            }
        }
    }
    return 0;
}
int main() {
    while(scanf("%d%d%d", &l, &r, &c) == 3) {
        if(!l && !r && !c) break;
        memset(d, 0, sizeof(d));
        for(int i = 0; i < l; i++) {
            for(int j = 0; j < r; j++) {
                scanf("%s", a[i][j]);
            }
        }
        for(int i = 0; i < l; i++) {
            for(int j = 0; j < r; j++) {
                for(int k = 0; k < c; k++) {
                    if(a[i][j][k] == S) {
                        sz = i; sx = j; sy = k;    
                    }
                    if(a[i][j][k] == E) {
                        ez = i; ex = j; ey = k;    
                    }
                }
            }
        }
        if(bfs()) printf("Escaped in %d minute(s).\n", ans);
        else printf("Trapped!\n");
    } 
    return 0;
}

 

以上是关于POJ 2251 Dungeon Master的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2251 Dungeon Master

Dungeon Master POJ-2251 三维BFS

POJ 2251 Dungeon Master

POJ 2251 Dungeon Master (BFS)

POJ-2251 Dungeon Master

POJ2251 Dungeon Master