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. 增加录像时间戳水印、 camera框架介绍

    http://blog.csdn.net/mirkerson/article/details/38920107 http://blog.csdn.net/jimbo_lee/article/detai ...

  2. 改动UITextfield的Placeholder字体的颜色

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  3. openwrt spi flash 分区适配过程

    openwrt spi flash 分区适配过程 这里基于 openwrt mt7620a 平台来跟踪,主要是想理清 dts 里的分区描述是如何一步步转化成内核分区行为. 先来看看 dts 中关于分区 ...

  4. iOS非常全的第三方库

    iOS ● 非常全的三方库.插件.大牛博客等等   github排名:https://github.com/trending, github搜索:https://github.com/search. ...

  5. aop学习总结一------使用jdk动态代理简单实现aop功能

    aop学习总结一------使用jdk动态代理实现aop功能 动态代理:不需要为目标对象编写静态代理类,通过第三方或jdk框架动态生成代理对象的字节码 Jdk动态代理(proxy):目标对象必须实现接 ...

  6. 【bzoj2588】Count on a tree 主席树

    这题给人开了个新思路. 原本构造一个序列的主席树,是这个位置用上个位置的信息来省空间,树上的主席树是继承父亲的信息来省空间. 此题若带修改怎么办? 若对某个点的权值做修改,则这个点的子树都会受影响,想 ...

  7. Android系统DHCP问题【转】

    本文转载自:http://blog.csdn.net/tankai19880619/article/details/42972551 一.现象 12小时压测wifi连接后,发现网络连接中断:相关log ...

  8. codeforces 的 Codeforces Round #273 (Div. 2) --C Table Decorations

    C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. javase练习题

    偶然看到一份javase的练习题,mark一下,以后练习下 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个 ...

  10. pypi指定下载源

    pip install -i http://pypi.douban.com/simple/ scipy==0.19.0