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. 在Fedora25上轻松安装Cuda8

    http://blog.csdn.net/u010158659/article/details/53592891 Cuda 8对于Pacal架构的英伟达新一代显卡(GTX 1070/1080/Tita ...

  2. Scala随记

    使用Scala首先确保本地Java 8版本,然后按照官网所说,比较流行的方式(1) sbt; (2) IDE "The most popular way to get Scala is ei ...

  3. OpenWrt:路由器上的Linux

    官网:https://openwrt.org/ 适于嵌入式设备的一个Linux发行版,可刷无线路由器. 相对原厂固件而言,OpenWrt不是一个单一.静态的固件,而是提供了一个可添加软件包的可写的文件 ...

  4. 在html中显示Flash的代码

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  5. iOS9新特性之泛型

     iOS9新特性之泛型 作用:限制类型 好处:1.提高开发规范,减少程序员之间的交流 2.通过集合取出来的对象,直接当作泛型对象使用,可以直接使用点语法(id类型不能使用点语法) 使用场景: 1.在集 ...

  6. IE浏览器 get请求缓存问题

    场景: 比较简单是使用的SpringMVC框架,在做资源国际化的时候,遇到了这个问题.具体做的操作是在页面上点击切换语言的时候,需要发起请求在Controller中切换Locale. 问题: 1.开始 ...

  7. java 传参方式--值传递还是引用传递

    java 传参方式--值传递还是引用传递 参数是按值而不是按引用传递的说明 Java 应用程序有且仅有的一种参数传递机制,即按值传递.写它是为了揭穿普遍存在的一种神话,即认为 Java 应用程序按引用 ...

  8. 不懂不能装懂--邮箱后缀“inc”的含义

    之前实习的公司,邮箱域名是XXX@XXX-inc.com 再之前,投递简历了或者关注某个公司的一些信息或者和这些公司的人邮件联系是也发现有这个inc的后缀,一直不明白,今天觉得自己找到答案了,不怕丢人 ...

  9. MysqlNDBcluster集群数据操作可能出现的问题

    Ndbcluster 版本7.5: 1.非ndbcluster引擎的表集群不会同步:若要同步,需要使engine=ndbcluster;如果表有外键约束需先删除外键,同步成功后再建立外键[否则会报错] ...

  10. ffmpeg遇到inttypes.h和UINT64_C

    http://blog.csdn.net/cll131421/article/details/7763657 编译过程:错误一:无法打开包括文件:“inttypes.h”: No such file ...