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,就是多加一组状态,需要细心(不细心如我就找了半个 ...
随机推荐
- R语言学习 第五篇:字符串操作
文本数据存储在字符向量中,字符向量的每个元素都是字符串,而非单独的字符.在R中,可以使用双引号,或单引号表示字符. 一,字符串中的字符数量 函数nchar()用于获得字符串中的字符数量: > s ...
- 11、Dockerfile实战-Tomcat
一.编写Dockerfile 具体步骤这里不再细说,直接看Dockerfile文件: FROM centos:7 MAINTAINER QUNXUE ENV VERSION=8.0.46 RUN yu ...
- Jenkins分布式构建
Jenkins分布式构建 有时,如果有一个实例,它是一个更大,更重的项目,需要定期编译生成在许多计算机上.并运行所有这些构建了中央台机器上可能不是最好的选择.在这种情况下,人们可以配置其他Jenkin ...
- OpenCV调整彩色图像的饱和度和亮度
问题 如何调整彩色图像的饱和度和亮度 解决思路 详细步骤: 将RGB图像值归一化到[0, 1] 然后使用函数cvtColor进行色彩空间的转换 接下来可以根据处理灰度图像对比度增强伽马变换或者线性变换 ...
- 《杜增强讲Unity之Tanks坦克大战》11-游戏流程控制
11 游戏流程控制 使用协程来控制游戏流程 11.1 添加MessageText 首先添加一个Text来显示文字 image 设置GameMgr image 11.2 游戏整体流程 下面Gam ...
- UE4添加植被Foliage Type
在UE4中的地形渲染上不可避免的需要添加植被,而如果采取手动添加StaticMesh植被的方式则会浪费大量的时间精力. UE4提供了一种批量添加地面植被类型的方式Foliage Type.在编辑器内容 ...
- 作业六:分析Linux内核创建一个新进程的过程
分析Linux内核创建一个新进程的过程 进程描述符PCB----task_struct数据结构 操作系统:1.进程管理 2.内存管理 3 文件系统 一.新进程如何创建和修改task_struct数据结 ...
- C#回调函数的简单讲解与应用例子(最简单讲解,大神绕道)
本博客一直以来的宗旨就是:用最简单的方式讲清楚不复杂的问题. 因为本人也很菜所以也没法讲太复杂HHHHHH...... 所以如果哪天某个大神看到了觉得讲的有问题欢迎指出. 话不多说进入正题.. ——— ...
- 作业一_随笔1_初来乍到:学号&博客地址
031302540——http://www.cnblogs.com/yyj031302540/ 计算机实验班叶艺洁
- 谈vs2013单元测试感想
(1)安装篇:这个就不用多说啦,百度一个安装包进行安装. 之前下载过vs2013当时是抱着玩玩的心态,也没有安装成功,现在作为作业重新安装,并且进行单元测试.下面就是安装vs2013的具体过程以及我遇 ...