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, 总共可以向6个方向走,走一步步数+1, 可以按行读取地图, 把z轴设为最高维度, 基本过程和二维的bfs类似
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std; char maze[][][];
bool v[][][];
int l, r, c; int dir[][] = {{,,},{-,,},{,,},{,-,},{,,},{,,-}}; struct node
{
int x, y, z, t;
}now, Next;
int bx, by, bz;
int ex, ey, ez; int bfs()
{
now.x = bx;
now.y = by;
now.z = bz;
now.t = ;
v[bz][bx][by] = ;
queue<node> q;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
if (now.z == ez && now.x == ex && now.y == ey)
return now.t;
for (int i = ; i < ; i++) {
Next.z = now.z + dir[i][];
Next.x = now.x + dir[i][];
Next.y = now.y + dir[i][];
Next.t = now.t + ;
if (Next.x>=&&Next.x<r&&Next.y>=&&Next.y<c&&Next.z>=&&Next.z<l
&& !v[Next.z][Next.x][Next.y] && maze[Next.z][Next.x][Next.y] != '#')
{
q.push(Next);
v[Next.z][Next.x][Next.y] = ;
}
}
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
while (~scanf("%d%d%d", &l, &r, &c)) {
if (l+c+r == ) break;
memset(v, , sizeof(v));
for (int i = ; i < l; i++)
for (int j = ; j < r; j++)
scanf("%s", maze[i][j]); for (int i = ; i < l; i++)
for (int j = ; j < r; j++)
for (int k = ; k < c; k++) {
if (maze[i][j][k] == 'S') {
bz = i; bx = j; by = k;
}
if (maze[i][j][k] == 'E') {
ez = i; ex = j; ey = k;
} }
int ans = bfs();
if (ans == )
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
} return ;
}
 

[poj] Dungeon Master bfs的更多相关文章

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

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

  2. POJ2251 Dungeon Master —— BFS

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

  3. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  4. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  5. poj 2251 Dungeon Master (BFS 三维)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  6. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

  7. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  8. Dungeon Master bfs

    time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...

  9. POJ2251 Dungeon Master(bfs)

    题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...

随机推荐

  1. OCR光学字符识别--STN-OCR 测试

    1.同文章中建议的使用ubuntu-python隔离环境,真的很好用 参照:http://blog.topspeedsnail.com/archives/5618启动虚拟环境:source env/b ...

  2. Flask:工厂函数和蓝本

    我们用pycharm去新建Flask项目的时候,会默认生成开发文件.如下,其中包括static,templates,flask1_prj.py文件 在最初开始的时候,我们的app等声明都是在flask ...

  3. SAP采购寄售业务操作步骤

    [转自 http://blog.sina.com.cn/s/blog_6466e5f70100jghg.html] 这里所示的是比较完整的步骤,包含了:信息记录.采购合同.货源清单.采购申请.采购订单 ...

  4. uboot 2013.01 代码简析(2)第一阶段初始化

    uboot执行"make smdk2410_config"之后就可以进行编译了,可以执行make命令进行编译, 因为整个输出太长,我仅仅列出部分最关键的输出(部分我不关心的内容直接 ...

  5. Excel图表转成图片

    关于excel 图表转成图片 知识点:excel 生成的图表不是图片 尝试.    通过Java调用POI接口挺难把excel生成的图表转成图片导出来 ps.      其它生成图表的工具,如jfre ...

  6. BZOJ 3624 [Apio2008]免费道路:并查集 + 生成树 + 贪心【恰有k条特殊路径】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3624 题意: 给你一个无向图,n个点,m条边. 有两种边,种类分别用0和1表示. 让你求一 ...

  7. win装wamp

    前传: 刚换了工作环境,从原来的全mac转到全windows,很不适应,简单的wamp鼓捣了半天 环境: win 7 旗舰版 ,wamp ,下载地址: http://pan.baidu.com/s/1 ...

  8. 【Opencv】Mat基础

    1.Mat::imread() C++: Mat imread(const string& filename, int flags=1 ) filename – Name of file to ...

  9. JVM内存溢出环境备份方法

    线上Tomcat服务内存溢出,且不容易重现,又没配置JMX监控端口,如何在不重启Tomcat的情况下备份堆dump和线程dump,进而分析原因? 因为Tomcat以服务模式运行,直接用JVisualV ...

  10. SQL 优化总结(三) SQL子句

    SQL子句 尽可能编写优化器可以优化的语句. 1. SELECT子句 (1) 在查询Select语句中用Where字句限制返回的行数,避免表扫描,如果返回不必要的数据,浪费了服务器的I/O资源,加重了 ...