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. 使用Python与数据库交互

    # -*- coding: utf-8 -*- """ Created on Sun Nov 18 19:25:01 2018 @author: wangm " ...

  2. IOS 单元测试

    本文转载至 http://blog.csdn.net/fengsh998/article/details/8109293 IOS 自带单元测试. 1.在创建时,将include Unit Tests钩 ...

  3. Hibernate表关系映射之多对多映射

    一.多对多的实现原理 在数据库中实现多对多的关系,必须使用连接表.也就是用一个独立的表来存入两个表的主键字段,通过遍历这张表来获取两表的关联关系. 而在我们的对象中,多对多是通过两者对象类中互相建立对 ...

  4. Eclipse内存错误java heap space

    Eclipse安装路径下的内存配置文件:eclipse.ini 文件末尾: -XX:MaxPermSize=256m-Xms40m-Xmx512m 其中 -Xmx512m表示最大内存,改为768或10 ...

  5. SQL 关联操作

  6. Handler向子线程发送数据

    public class MainActivity extends AppCompatActivity { private static final String TAG = "MainAc ...

  7. Android Weekly Notes Issue #240

    Android Weekly Issue #240 January 15th, 2017 Android Weekly Issue #240 Hello, 各位亲, 从本篇笔记开始, 以后并不包含An ...

  8. raise 与 raise ... from 的区别

    起步 Python 的 raise 和 raise from 之间的区别是什么? try: print(1 / 0) except Exception as exc: raise RuntimeErr ...

  9. Nginx安装教程(Centos6.8)

    1.安装gcc gcc-c++(如新环境,未安装请先安装 yum install -y gcc gcc-c++ 2.安装wget yum -y install wget 3.安装PCRE库 cd /h ...

  10. 在jboss中部署可执行jar, deploy executable jar in jboss

    首先,题目是个伪命题, jboss容器是不支持直接部署可执行jar包的,jar只会被加载当作lib对待.这里提供了一个小的变通方案. 今天我遇到个问题,把我们的项目中的监控模块独立成一个小项目部署,监 ...