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. xcode8.0升级之后公司项目遇到的问题

    xcode8升级之后项目遇到了问题,由于这个项目是我中途接手的,遇到的第三方也是自己没有用过的, AQGridViewCell,这个第三方的类主要是用于处理图片的问题,xcode开发工具升级过后,报这 ...

  2. docker通过iptables修改或新增镜像映射端口

    443 8088 22 端口是初始映射端口 [root@SERVER ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAM ...

  3. GCC 预处理、编译、汇编、链接..

    1简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objective ...

  4. css 垂直居中

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. [Erlang 0117] 当我们谈论Erlang Maps时,我们谈论什么 Part 2

    声明:本文讨论的Erlang Maps是基于17.0-rc2,时间2014-3-4.后续Maps可能会出现语法或函数API上的有所调整,特此说明. 前情提要: [Erlang 0116] 当我们谈论E ...

  6. MySQL 导入数据

    MySQL中可以使用两种简单的方式来导入MySQL导出的数据. 使用 LOAD DATA 导入数据 MySQL 中提供了LOAD DATA INFILE语句来插入数据. 以下实例中将从当前目录中读取文 ...

  7. inotify+rsync实现实时同步部署

    1.1.架构规划 1.1.1架构规划准备 服务器系统 角色 IP Centos6.7 x86_64 NFS服务器端(NFS-server-inotify-tools) 192.168.1.14 Cen ...

  8. python-socket模块

    socket server #!/usr/bin/env python # -*- coding:utf-8 -*- import socket ip_port = ('127.0.0.1',9999 ...

  9. chpasswd命令

    chpasswd命令是批量更新用户口令的工具,是把一个文件内容重新定向添加到/etc/shadow中.   语法 chpasswd(选项) 选项 -e:输入的密码是加密后的密文:   -h:显示帮助信 ...

  10. 【python之路2】CMD中执行python程序中文显示乱码

    在IDLE中执行下面代码,中文显示正常: # -*- coding:utf-8 -*- st=raw_input("请输入内容")print st 但在CMD中执行e:\hello ...