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,就是多加一组状态,需要细心(不细心如我就找了半个 ...
随机推荐
- [Oracle]查看数据是否被移入 DataBuffer 的方法
查看数据是否被移入 DataBuffer 的方法: 例如:表名为 tabxxx, 用户为U2: SQL> grant dba to u2 identified by u2;SQL> con ...
- iOSPush自动隐藏tabbar
只需要在UITabBarController添加控制器的时候调用YZNav初始化,就可以实现tabbar的自动隐藏了. 直接上github地址:https://github.com/YouZhiZhe ...
- ReactJS实用技巧(1):JSX与HTML的那些不同
在项目中使用ReactJS也已经有大半年了,收获很多也踩过不少坑.不想把这个系列写成抄书似的罗列,旨在总结些常用的技巧及常见的坑,以帮助初心者快速入门,想系统学习的同学还是多阅读文档. JSX本质上与 ...
- git 跟踪提交记录
一.克隆git仓库 git clone ssh://hwl@xxx/home/data/repositories/git.git 二.申明使用人信息,以便跟踪提交记录 $ git config --g ...
- linux centos 中Tomcat的安装和自启动配置
Tomcat的安装和自启动配置将tomcat添加为linux系统服务,网上找到了很多方法,其中比较简单的如下:方法一:(亲测有效)1. 首先需要将$Tomcat_HOME/bin目录下的catalin ...
- 现已告别五险一金?迎来社保商保时代保险INSURAUNCE
现已告别五险一金?迎来社保商保时代保险INSURAUNCE 经济工作会议提出,中国要降低社会保险费,研究精简归并"五险一金",可以说是为社保变革指明了大方向.未来,生育保险将与基本 ...
- PAT甲题题解-1117. Eddington Number(25)-(大么个大水题~)
如题,大水题...贴个代码完事,就这么任性~~ #include <iostream> #include <cstdio> #include <algorithm> ...
- B. Lost Array
链接 [http://codeforces.com/contest/1043/problem/B] 题意 自己点开链接看 分析 1到n枚举某个值,判断是否满足并统计 判断方法:假设x序列成立,那么后面 ...
- LeetCode 141. Linked List Cycle环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...
- Daily Scrum NO.6
会议概况 这两日又是由于编译deadline和数据库课程设计使得我们的进度进行缓慢.但是项目的进程仍然在可掌控的范围之内,时间虽然紧,但是应该最终能够实现Beta版本. 这次会议我们总结了之前5个正常 ...