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. htm常用标签总结

    1.结构性定义 文件类型 <HTML></HTML> (放在档案的开头与结尾) 文件主题 <TITLE></TITLE> (必须放在「文头」区块内) 文 ...

  2. JQuery使用deferreds串行多个ajax请求

    使用JQuery对多个ajax请求串行执行. HTML代码: <a href="#">Click me!</a> <div></div&g ...

  3. VS2012 Unit Test 个人学习汇总(含目录)

    首先,给出MSDN相关地址:http://msdn.microsoft.com/en-us/library/Microsoft.VisualStudio.TestTools.UnitTesting.a ...

  4. 实用的圆形图片控件ImageView

    1.用法直接在布局中引用即可 import android.content.Context;import android.content.res.TypedArray;import android.g ...

  5. MySQL可视化软件Work Bench导出导入数据库

    首先打开你的work bench,输入你的密码进入主页面 A:导入数据库 在Schemas空白处右键选择Create~:建立一个数据库,然后就可以导入你的sql文件了 File-->Open S ...

  6. Hibernate 系列 05 - Session 类

    引导目录: Hibernate 系列教程 目录 前言: Session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库的存取都与Session息息相关. 就如同在编写JDBC时需要关 ...

  7. Webform(六)——登录状态保持(Cookies内置对象)

    用户用浏览器访问一个网站,由于采用的http的特性,Web服务器并不能知道是哪一个用户正在访问,但一些网站,希望能够知道访问者的一些信息,例如是不是第一次访问,访问者上次访问时是否有未做完的工作,这次 ...

  8. utf-8 汉字对照表

    之前从redis中取出一些数据,utf8 16进制编码,想转成字符,没有找到现成的转化工具,先用这个表直接查找对照吧. UTF8编码表大全Code code# Code (coded in UTF-8 ...

  9. Django基础,Day1 - 环境安装与pycharm创建django项目

    Django是一个高级Python Web框架,支持快速部署,清理和实用的设计.它可以被轻易部署和提供实用的组件,而开发人员只需要专注于写自己的应用程序,而不需要重复造轮子.并且Django是自由和开 ...

  10. TypeScript 素描 - 类

    本文虽然是学自官方教程而来,但是也融入了自己的理解,而且对官方的例子做了一些修改 /* 类 面向对象编程的一大核心 使用C#.Java进行编程的朋友肯定已经是不能够再熟悉了 TypeScript的类与 ...