Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32228   Accepted: 12378

http://poj.org/problem?id=2251

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! 普通的bfs迷宫题,只是增加了第3维。
 #include<iostream>
#include<queue>
#include<cstring>
using namespace std; const int MAX=;
struct Node
{
char c;
int pace,z,x,y;//z,x,y--楼层,行,列
}maze[MAX][MAX][MAX];//存储迷宫字符,坐标,到达步数
int L,R,C;
int dre[][]={{,,},{-,,},{,,},{,-,},{,,},{,,-}};//方向:东南西北上下
bool check(int z,int x,int y)//检查坐标是否超出范围
{
return (z>=&&z<=L&&x>=&&x<=R&&y>=&&y<=C);
}
int bfs(int z0,int x0,int y0)//找到从点[z0][x0][y0]到终点的最小步数
{
queue<Node> Q;
Node t;
Q.push(maze[z0][x0][y0]);//放入起点
while(!Q.empty())
{
t=Q.front();
Q.pop();
if(t.c=='E') return t.pace;//找到,返回步数
for(int i=;i<;i++)//枚举6个方向,若可以走且未走过就push到队尾
{
int Z=t.z+dre[i][],X=t.x+dre[i][],Y=t.y+dre[i][];
if(check(Z,X,Y))
{
if(maze[Z][X][Y].c!='#'&&!maze[Z][X][Y].pace)
{
maze[Z][X][Y].pace=t.pace+;//步数等于上一步(t)的步数加1
Q.push(maze[Z][X][Y]);
}
}
}
}
return -;//找不到返回-1
}
int main()
{
while(cin>>L>>R>>C,L||R||C)
{
int sz,sx,sy;
memset(maze,,sizeof(maze));
for(int i=; i<L; i++)
for(int j=; j<R; j++)
for(int k=; k<C; k++)
{
cin>>maze[i][j][k].c;
maze[i][j][k].z=i;
maze[i][j][k].x=j;
maze[i][j][k].y=k;
if(maze[i][j][k].c=='S')//记录起点
{
sz=i;
sx=j;
sy=k;
}
}
int re=bfs(sz,sx,sy);
if(re!=-) cout<<"Escaped in "<<re<<" minute(s).\n";
else cout<<"Trapped!\n";
}
return ;
}

bfs—Dungeon Master—poj2251的更多相关文章

  1. Dungeon Master POJ-2251 三维BFS

    题目链接:http://poj.org/problem?id=2251 题目大意 你被困在了一个三维的迷宫,找出能通往出口的最短时间.如果走不到出口,输出被困. 思路 由于要找最短路径,其实就是BFS ...

  2. BFS POJ2251 Dungeon Master

    B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. POJ2251 Dungeon Master —— BFS

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

  4. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

  5. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

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

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

  7. POJ 2251 Dungeon Master (非三维bfs)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55224   Accepted: 20493 ...

  8. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  9. POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48380   Accepted: 18252 ...

随机推荐

  1. Github基础使用教程 ———功能介绍

    Github基础使用手把手教程    --功能介绍 本人Github小白,刚摸索的差不多,记录一下经验,小白写出来的东西各位萌新一定看的懂啦~ 本篇内容主要针对想快速学会使用Github这个强大工具的 ...

  2. itoa、ltoa

    #include <stdlib.h> /*整形转字符型*/ char * itoa(int value, char *string, int radix) { char tmp[33]; ...

  3. 【python实现卷积神经网络】优化器的实现(SGD、Nesterov、Adagrad、Adadelta、RMSprop、Adam)

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  4. Netty:ChannelFuture

    上一篇我们完成了对Channel的学习,这一篇让我们来学习一下ChannelFuture. ChannelFuture的简介 ChannelFuture是Channel异步IO操作的结果. Netty ...

  5. 数论-质因数(gcd) UVa 10791 - Minimum Sum LCM

    https://vjudge.net/problem/UVA-10791/origin 以上为题目来源Google翻译得到的题意: 一组整数的LCM(最小公倍数)定义为最小数,即 该集合的所有整数的倍 ...

  6. (转) POJO和javabean的异同

    参考:http://blog.csdn.net/lushuaiyin/article/details/7436318 一:什么是POJOPOJO的名称有多种,pure old java object ...

  7. .net批量更新(插入、修改、删除)数据库

    思路: 1. 设置DataTable中每行的状态标识,即调用DataRow的方法setAdded().setModified().Delete() 2. 使用DataAdapter的Update(Da ...

  8. 通过 Swoole\Table 实现 Swoole 多进程数据共享

    第三方存储媒介 前面我们介绍了基于 Swoole 的 Process 及 Process\Pool 模块在 PHP 中实现多进程管理,但是多进程模式下进程间是相互隔离的,无法共享数据和变量,即便是通过 ...

  9. PHP反序列化漏洞总结

    写在前边 做了不少PHP反序列化的题了,是时候把坑给填上了.参考了一些大佬们的博客,自己再做一下总结 1.面向对象 2.PHP序列化和反序列化 3.PHP反序列化漏洞实例 1.面向对象 在了解序列化和 ...

  10. Python数据分析:大众点评数据进行选址

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:砂糖侠 如果你处于想学Python或者正在学习Python,Pyth ...