POJ - 2251 Dungeon Master 多维多方向BFS
Dungeon Master
Is an escape possible? If yes, how long will it take?
Input
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
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的更多相关文章
- poj 2251 Dungeon Master 3维bfs(水水)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21230 Accepted: 8261 D ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- 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 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48380 Accepted: 18252 ...
随机推荐
- 使用Python与数据库交互
# -*- coding: utf-8 -*- """ Created on Sun Nov 18 19:25:01 2018 @author: wangm " ...
- IOS 单元测试
本文转载至 http://blog.csdn.net/fengsh998/article/details/8109293 IOS 自带单元测试. 1.在创建时,将include Unit Tests钩 ...
- Hibernate表关系映射之多对多映射
一.多对多的实现原理 在数据库中实现多对多的关系,必须使用连接表.也就是用一个独立的表来存入两个表的主键字段,通过遍历这张表来获取两表的关联关系. 而在我们的对象中,多对多是通过两者对象类中互相建立对 ...
- Eclipse内存错误java heap space
Eclipse安装路径下的内存配置文件:eclipse.ini 文件末尾: -XX:MaxPermSize=256m-Xms40m-Xmx512m 其中 -Xmx512m表示最大内存,改为768或10 ...
- SQL 关联操作
- Handler向子线程发送数据
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainAc ...
- Android Weekly Notes Issue #240
Android Weekly Issue #240 January 15th, 2017 Android Weekly Issue #240 Hello, 各位亲, 从本篇笔记开始, 以后并不包含An ...
- raise 与 raise ... from 的区别
起步 Python 的 raise 和 raise from 之间的区别是什么? try: print(1 / 0) except Exception as exc: raise RuntimeErr ...
- Nginx安装教程(Centos6.8)
1.安装gcc gcc-c++(如新环境,未安装请先安装 yum install -y gcc gcc-c++ 2.安装wget yum -y install wget 3.安装PCRE库 cd /h ...
- 在jboss中部署可执行jar, deploy executable jar in jboss
首先,题目是个伪命题, jboss容器是不支持直接部署可执行jar包的,jar只会被加载当作lib对待.这里提供了一个小的变通方案. 今天我遇到个问题,把我们的项目中的监控模块独立成一个小项目部署,监 ...