Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28416   Accepted: 11109

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迷宫,给你起点终点,找出最短路径。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn =  35;
char maze[maxn][maxn][maxn];
int dis[maxn][maxn][maxn];
int L, R, C;

struct Node
{
    int z, x, y;
    Node(int zz, int xx, int yy): z(zz), x(xx), y(yy) {}
};

int bfs(int sz, int sx, int sy, int gz, int gx, int gy)
{
    queue<Node>que;
    int dx[5] = { -1, 0, 1,0}, dy[5] = {0, -1, 0, 1}, dz[3] = {0, -1, 1};
    memset(dis, INF, sizeof(dis));

    dis[sz][sx][sy] = 0;
    que.push(Node(sz, sx, sy));

    while (!que.empty())
    {
        Node pos = que.front();
        que.pop();

        if (pos.z == gz && pos.x == gx && pos.y == gy)
        {
            break;
        }

        for (int i = 0; i < 3; i++)
        {
            if (i)
            {
                int nz = pos.z + dz[i], nx = pos.x, ny = pos.y;
                if (nz >= 0 && nz < L && nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nz][nx][ny] != '#' && dis[nz][nx][ny] == INF)
                {
                    que.push(Node(nz, nx, ny));
                    dis[nz][nx][ny] = dis[pos.z][pos.x][pos.y] + 1;
                }
            }
            else
            {
                for (int j = 0; j < 5; j++)
                {
                    int nz = pos.z + dz[i], nx = pos.x + dx[j], ny = pos.y + dy[j];
                    if (nz >= 0 && nz < L && nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nz][nx][ny] != '#' && dis[nz][nx][ny] == INF)
                    {
                        que.push(Node(nz, nx, ny));
                        dis[nz][nx][ny] = dis[pos.z][pos.x][pos.y] + 1;
                    }
                }
            }

        }
    }
    return dis[gz][gx][gy];
}

int main()
{
    //freopen("input.txt", "r", stdin);
    while (~scanf("%d%d%d", &L, &R, &C) && L && R && C)
    {
        int sz, sx, sy, gz, gx, gy;
        for (int i = 0; i < L; i++)
        {
            for (int j = 0; j < R; j++)
            {
                scanf("%s", maze[i][j]);
                for (int k = 0; k < C; k++)
                {
                    if (maze[i][j][k] == 'S')
                    {
                        sz = i, sx = j, sy = k;
                    }
                    if (maze[i][j][k] == 'E')
                    {
                        gz = i, gx = j, gy = k;
                    }
                }
            }
            getchar();
        }
        bfs(sz, sx, sy, gz, gx, gy) == INF ? printf("Trapped!\n") : printf("Escaped in %d minute(s).\n", dis[gz][gx][gy]);
    }
    return 0;
}

  

POJ 2251 Dungeon Master(3D迷宫 bfs)的更多相关文章

  1. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  2. POJ 2251 Dungeon Master【三维BFS模板】

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...

  3. poj 2251 Dungeon Master 3维bfs(水水)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21230   Accepted: 8261 D ...

  4. POJ 2251 Dungeon Master(三维空间bfs)

    题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue& ...

  5. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  6. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  7. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  8. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

  9. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  10. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. SQL中varchar和nvarchar的区别

    varchar(n)长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为输入数据的字节的实际长度,而不是 n 个字节. nvar ...

  2. JQuery中隐藏/显示事件函数

    1.$("button").click(function(){ $("p").hide(); });2.如果您的网站包含许多页面,并且您希望您的 jQuery ...

  3. 深度剖析 | 基于大数据架构的BI应用

    说起互联网.电商的数据分析,更多的是谈应用案例,如何去实践数据化管理运营.而这里,我们要从技术角度分享关于数据的技术架构干货,如何应用BI. 原文是云猴网BI总经理王卫东在帆软大数据上的演讲,以下是整 ...

  4. 检索Google Maps地图位置(小训练)

    名称:检索地图位置 内容:地图初期显示和检索显示 功能:根据条件检索地图的经度与纬度 1.在这之前我们需要创建一个表(Accoun__c),添加一个重要的字段地理位置情報,它会默认的给你两个字段经度和 ...

  5. Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead的解决办法

    今天在导入工程进Eclipse的时候竟然出错了,控制台输出的是: [2013-02-04 22:17:13 - takepicture] Android requires compiler compl ...

  6. CSS实现内容超过长度后以省略号显示

    样式: {width: 160px; overflow: hidden; text-overflow:ellipsis; white-space: nowrap;} 说明: white-space: ...

  7. mysql 常用自定义函数解析

    -- /* -- * 用于获取一记录数据,根据传入的分隔字符delim,索引位置pos,返回相对应的value -- * SELECT Json_getKeyValue({"A": ...

  8. java获取日期之间天数的方法

    //获取两个日期之间的天数private int daysBetween(Date now, Date returnDate) { Calendar cNow = Calendar.getInstan ...

  9. eclipse 启动到loading workbench... 自动关闭

    是由于项目没有正常关闭运行而导致"workbench.xmi"中的"persistedState"标签还保持在运行时的配置造成的. 解决方法: 找到<wo ...

  10. [转]用Middleware给ASP.NET Core Web API添加自己的授权验证

    本文转自:http://www.cnblogs.com/catcher1994/p/6021046.html Web API,是一个能让前后端分离.解放前后端生产力的好东西.不过大部分公司应该都没能做 ...