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 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!
Http
POJ:https://vjudge.net/problem/POJ-2251
Source
广度优先搜索
题目大意
在一个三维的迷宫中从起点走到终点
解决思路
广度优先搜索,向六个方向延伸
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxN=35;
const int inf=2147483647;
const int F1[10]={0,1,-1,0,0,0,0};
const int F2[10]={0,0,0,1,-1,0,0};
const int F3[10]={0,0,0,0,0,1,-1};
class Position
{
public:
int x,y,z;
int step;
};
int a,b,c;
int Map[maxN][maxN][maxN];
queue<Position> Q;
bool vis[maxN][maxN][maxN];
int main()
{
while (cin>>a>>b>>c)
{
if ((a==0)&&(b==0)&&(c==0))
break;
memset(Map,-1,sizeof(Map));
memset(vis,0,sizeof(vis));
while (!Q.empty())
Q.pop();
int x0,y0,z0,x1,y1,z1;
char str[maxN];
for (int i=1;i<=a;i++)
for (int j=1;j<=b;j++)
{
cin>>str;
for (int k=0;k<c;k++)
{
if (str[k]!='#')
Map[i][j][k+1]=1;
if (str[k]=='S')
{
x0=i;
y0=j;
z0=k+1;
}
else
if (str[k]=='E')
{
x1=i;
y1=j;
z1=k+1;
}
}
}
/*for (int i=1;i<=a;i++)
{
for (int j=1;j<=b;j++)
{
for (int k=1;k<=c;k++)
cout<<Map[i][j][k]<<' ';
cout<<endl;
}
cout<<endl;
}*/
Q.push((Position){x0,y0,z0,0});
vis[x0][y0][z0]=1;
bool is_get=0;
do
{
Position u=Q.front();
Q.pop();
//cout<<u.x<<' '<<u.y<<' '<<u.z<<' '<<u.step<<endl;
if ((u.x==x1)&&(u.y==y1)&&(u.z==z1))
{
is_get=1;
//out<<u.step<<endl;
printf("Escaped in %d minute(s).\n",u.step);
break;
}
for (int i=1;i<=6;i++)
{
int x2=u.x+F1[i];
int y2=u.y+F2[i];
int z2=u.z+F3[i];
if ((Map[x2][y2][z2]!=-1)&&(vis[x2][y2][z2]==0))
{
//cout<<"("<<u.x<<","<<u.y<<','<<u.z<<")->("<<x2<<","<<y2<<","<<z2<<")"<<endl;
Q.push((Position){x2,y2,z2,u.step+1});
vis[x2][y2][z2]=1;
}
}
}
while (!Q.empty());
if (is_get==0)
cout<<"Trapped!"<<endl;
}
return 0;
}
POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)的更多相关文章
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- ZOJ 1940 Dungeon Master【三维BFS】
<题目链接> 题目大意: 在一个立体迷宫中,问你从起点走到终点的最少步数. 解题分析: 与普通的BFS基本类似,只需要给数组多加一维,并且走的时候多加 上.下这两个方向就行. #inclu ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ - 2251 Dungeon Master 多维多方向BFS
Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is ...
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- 【BFS】POJ 2251
POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...
随机推荐
- python3解析网页经过base64编码后的图片
有时候我们打开网页看到的图片不是普通的url,例如:www.baidu.com/static/2.jpg,而是经过base64方式加密过的路径:例如:data:img/jpg;base64,/9j/4 ...
- 在HTML中为JavaScript传递变量
在html中为JavaScript传递变量是一个关键步骤,然后就可以通过对JavaScript变量操作,实现想要达到的目的 本节代码主要使用了JavaScript中的document对象中的getEl ...
- 《Pro SQL Server Internals, 2nd edition》的CHAPTER 2 Tables and Indexes中的Clustered Indexes一节(翻译)
<Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress出版年: 2016-12-29页数: 804定价: USD 59 ...
- linux一切皆文件之块设备文件(四)
一.知识准备 1.在linux中,一切皆为文件,所有不同种类的类型都被抽象成文件(比如:块设备,socket套接字,pipe队列) 2.操作这些不同的类型就像操作文件一样,比如增删改查等 3.块设备是 ...
- A1020. Tree Traversals(25)
这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列. 这题的核心: 建树 BFS #include<bits/stdc++.h> using names ...
- 互联网寒冬,Python 程序员如何准备面试
虽说年年都在喊互联网寒冬,最难就业季,但是今年确实有点不一样,年前年后一波又一波互联网公司宣布『人员调整,结构优化』, 这是往年没发生过的. 是不是面试机会就少了很多呢?不是的. 搜索招聘网站我们可以 ...
- python中eval方法的使用
eval函数就是实现list.dict.tuple与str之间的转化str函数把list,dict,tuple转为为字符串# 字符串转换成列表a = "[[1,2], [3,4], [5,6 ...
- 第二阶段冲刺——four
个人任务: 季方:实现团队博客作业查询. 王金萱:优化统计团队博客结果界面的显示. 马佳慧:选择功能界面的背景设计. 司宇航:用servlet完成名单打印功能. 站立会议: 任务看板和燃尽图:
- git hub 使用心得
git中重要的概念: 工作目录(working directory):在工作目录中修改文件,修改后的文件状态是modified,新添加的文件是untracked,通过git add命令将文件保存到st ...
- [Docker基础]如何清除不用的资源
Docker - How to cleanup resources 有时你可能需要清理Docker中不用的资源,特别是在学习Docker过程中创建的镜像.容器.网络.存储卷等. delete volu ...