Dungeon Master

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! 之前做过类似的A计划,那道题是二维传送地图,当时是用DFS写的。这次的Dungeon Master是A计划的升级版,达到30的多维地图(WA:只能传送到相邻维度。。),所以尝试DFS果断超时,,然后重码了一遍BFS-_-16msA掉。这再一次地证明了在处理最短路径时BFS的效率之高。 DFS TLE代码:
#include<stdio.h>
#include<string.h> char a[][][];
int b[][][];
int t[][]={{,,},{,,},{,-,},{,,-},{,,},{-,,}};
int ww,n,m,bw,bx,by,min; void dfs(int w,int x,int y,int s)
{
int i;
if(w<||x<||y<||w>=ww||x>=n||y>=m) return;
if(a[w][x][y]=='E'){
if(s<min) min=s;
return;
}
if(a[w][x][y]=='#'||b[w][x][y]==) return;
for(i=;i<;i++){
int tw=w+t[i][];
int tx=x+t[i][];
int ty=y+t[i][];
b[w][x][y]=;
dfs(tw,tx,ty,s+);
b[w][x][y]=;
}
} int main()
{
int i,j,k;
while(scanf("%d%d%d",&ww,&n,&m)&&!(ww==&&n==&&m==)){
for(i=;i<ww;i++){
for(j=;j<n;j++){
getchar();
scanf("%s",a[i][j]);
for(k=;k<m;k++){
if(a[i][j][k]=='S'){
bw=i;
bx=j;
by=k;
}
}
}
}
min=;
memset(b,,sizeof(b));
dfs(bw,bx,by,);
if(min==) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",min);
}
return ;
}

BFS AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][][];
int b[][][];
int t[][]={{,,},{,,},{,-,},{,,-},{,,},{-,,}}; struct Node{
int w,x,y,s;
}node; int main()
{
int ww,n,m,i,j,k;
while(scanf("%d%d%d",&ww,&n,&m)&&!(ww==&&n==&&m==)){
queue<Node> q;
memset(b,,sizeof(b));
for(i=;i<ww;i++){
for(j=;j<n;j++){
getchar();
scanf("%s",a[i][j]);
for(k=;k<m;k++){
if(a[i][j][k]=='S'){
b[i][j][k]=;
node.w=i;
node.x=j;
node.y=k;
node.s=;
q.push(node);
}
}
}
}
int f=;
while(q.size()){
for(i=;i<;i++){
int tw=q.front().w+t[i][];
int tx=q.front().x+t[i][];
int ty=q.front().y+t[i][];
if(tw<||tx<||ty<||tw>=ww||tx>=n||ty>=m) continue;
if(a[tw][tx][ty]=='E'){
f=q.front().s+;
break;
}
if(a[tw][tx][ty]=='#'||b[tw][tx][ty]==) continue;
b[tw][tx][ty]=;
node.w=tw;
node.x=tx;
node.y=ty;
node.s=q.front().s+;
q.push(node);
}
if(f!=) break;
q.pop();
}
if(f==) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",f);
}
return ;
}

POJ - 2251 Dungeon Master 多维多方向BFS的更多相关文章

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

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

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

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

  3. 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 ...

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

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

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

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

  6. BFS POJ 2251 Dungeon Master

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

  7. POJ 2251 Dungeon Master (三维BFS)

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

  8. poj 2251 Dungeon Master

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

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

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

随机推荐

  1. kubernetes之StatefulSet详解

    系列目录 概述 RC.Deployment.DaemonSet都是面向无状态的服务,它们所管理的Pod的IP.名字,启停顺序等都是随机的,而StatefulSet是什么?顾名思义,有状态的集合,管理所 ...

  2. 未开启HugePages ORACLE session剧增时引起的一次悲剧

    故障简单描写叙述一下:LINUX系统未开启HugePages,主机内存将近300G.SWAP是32G.ORACLE 的 SGA_MAX_SIZE设置是主机内存的将近80%,SGA_TARGET设置是主 ...

  3. thinkphp3.2独立分组的建立

    很简单,就是把默认的Home模块复制一份,放到Admin目录下,同时把namespace改成namespace Admin\Controller即可,配置项如下:

  4. 剑指Offer:树的子结构【26】

    剑指Offer:树的子结构[26] 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 解题思路 分为两步: 第一步:在树A中找到和树B的根节点的值一 ...

  5. 关于node.js的安装与删除

    安装node.js 先切换到root用户安装 openssl-devel su - yum install openssl-devel 下载源代码自己编译以下代码中的tar.gz包根据node.js官 ...

  6. Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学

    题目链接:http://codeforces.com/problemset/problem/451/D D. Count Good Substrings time limit per test 2 s ...

  7. Linux系统中10个常用的ps命令总结

    Linux作为Unix的衍生操作系统,Linux内建有查看当前进程的工具ps.这个工具能在命令行中使用PS 命令是什么 查看它的man手册可以看到,ps命令能够给出当前系统中进程的快照.它能捕获系统在 ...

  8. Transaction事务注解和DynamicDataSource动态数据源切换问题解决

    问题描述: 写主库开事务的情况下会导致时不时的将更新/插入操作写入到从库上, 导致mysqlException update command denied   问题原因: jetty的工作队列会重用处 ...

  9. Android记录程序崩溃Log写入文件

    将导致程序崩溃的堆栈调用Log写入文件,便于收集bug.在调试安卓程序,由于某些原因调试时手机不能连接PC端,无法通过IDE查看程序崩溃的Log,希望log能够写入文件中,对于已经发布的App可以通过 ...

  10. BluetoothLE-Multi-Library 一个能够连接多台蓝牙设备的库,它可以作为client端,也可以为server端。支持主机/从机,外围设备连接。

    github地址:https://github.com/qindachang/BluetoothLE-Multi-Library BluetoothLE-Multi-Library 一个能够连接多台蓝 ...