POJ-2251-Dungeon Master(3D迷宫,BFS)
Dungeon Master
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 48111 | Accepted: 18149 |
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!
三维迷宫,跟二维迷宫思路差不多;
#include <iostream>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
char MAP[35][35][35];
bool visit[35][35][35];
int dir[6][3] =
{
{ 0, -1, 0}, //北
{ 0, 1, 0}, //南
{-1, 0, 0}, //西
{ 1, 0, 0}, //东
{ 0, 0, 1}, //上
{ 0, 0, -1}, //下
};
//int M[35][35][35];
int l, r, c;
struct MiGong
{
int x, y, z;
int way;
};
//MiGong dist[35][35][50];
MiGong v;
int k = 0;
void BFS ( int x, int y, int z )
{
queue <MiGong> Q;
visit[x][y][z] = true;
v.x = x;
v.y = y;
v.z = z;
v.way = 0;
Q.push( v );
while( !Q.empty() )
{
v = Q.front();
Q.pop();
if( MAP[v.x][v.y][v.z] == 'E' )
break;
MiGong now;
for( int i=0; i<6; i++ )
{
now.x = v.x + dir[i][0];
now.y = v.y + dir[i][1];
now.z = v.z + dir[i][2];
if( now.x >= 0 && now.y >= 0 && now.z >= 0 && now.x <= 30 && now.y <= 30 && now.z <= 30 )
if( !visit[now.x][now.y][now.z] && (MAP[now.x][now.y][now.z] == '.' || MAP[now.x][now.y][now.z] == 'E'))
{
//dist[now.x][now.y][now.z].way = dist[v.x][v.y][v.z].way + 1;
now.way = v.way + 1;
visit[now.x][now.y][now.z] = true;
Q.push( now );
}
}
}
if( !v.way )
cout << "Trapped!" << endl;
else
cout << "Escaped in " << v.way <<" minute(s)." << endl;
//cout << v.way << endl;
}
int main()
{
int i, j, k;
while(cin >> l >> r >> c && l && r && c )
{
memset( MAP, 0, sizeof(MAP));
memset( visit, 0, sizeof(visit));
int x = -1, y = -1, z = -1;
for( i=0; i<l; i++ )
for( j=0; j<r; j++ )
for( k=0; k<c; k++ )
{
cin >> MAP[i][j][k];
if( MAP[i][j][k] == 'S')
{
x = i;
y = j;
z = k;
}
}
BFS( x, y, z );
}
return 0;
}
POJ-2251-Dungeon Master(3D迷宫,BFS)的更多相关文章
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ 2251 Dungeon Master【三维BFS模板】
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...
- 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)
题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue& ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- 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)
题目链接: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 ...
随机推荐
- Android TV上的焦点切换效果
转载:http://blog.csdn.net/wzlas111/article/details/39741091 Android TV上的焦点凸显特效相信大家都看到过,那么我们就来实现它吧,首先上张 ...
- 面向对象的JavaScript-001
一. Question是父类,MultipleChoiceQuestion和DragDropQuestion是子类 二. 1. <script> // 面向对象 function Ques ...
- 访问tomcat出现java.lang.IllegalStateException No output folder错误解决方法
访问tomcat出现java.lang.IllegalStateException: No output folder错误解决方法 问题:tomcat分为安装版和解压缩版,解压缩版如果解压到安装盘,在 ...
- p4364 [九省联考2018]IIIDX
传送门 分析 我们先考虑如果所有数都不相同我们应该怎么办 我们可以直接贪心的在每个点放可行的最大权值 但是题目要求可以有相同的数 我们可以考虑每次让当前节点可发且尽量大的同时给兄弟节点留的数尽量大 我 ...
- python3--多目录之间的协作的一些必备知识
# Auther: Aaron Fan # 动态获取执行文件的相对路径路径:print(__file__) #动态获取执行文件的绝对路径:import osfile_path = os.path.ab ...
- Smarty配置与实例化
在smarty文件夹下建立一个test文件夹,test下建立如下: 编辑test.php如下: <?php require('../smarty/Smarty.class.php'); $sma ...
- C#开发重用方法
获取类型先关信息 GetType()及typeof()
- HDU 6153 A Secret (KMP)
题意:给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数. 析:首先先把两个串进行反转,这样后缀就成了前缀.然后求出 s 的失配函数,然后在 t 上跑一遍,如果发现不匹配了或者是已经 ...
- Word2013文章如何直接发布到CSDN博客
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- OpenCV4Android安装
转:http://blog.csdn.net/gao_chun/article/details/49359535 1.下载及目录介绍 2.将 OpenCV引入 Android Studio 3.更新 ...