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 ...
随机推荐
- 微信小程序开发教程,大多数人都搞错的八个问题
小程序目前被炒得沸沸扬扬,无数媒体和企业借机获取阅读流量. 这再次证明一点,微信想让什么火,真的就能让什么火.这种能力真是全中国再也没有人有了,政府也没有. 但四处传的消息很多是失真的,废话不说,先列 ...
- 面向对象的JavaScript-007-Function.prototype.bind() 的4种作用
1. // Function.prototype.bind() 的作用 // 1.Creating a bound function this.x = 9; var module = { x: 81, ...
- String.getBytes()[转]
在Java中,String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组.这个表示在不通OS下,返回的东西不一样! String.getBytes(String decode)方 ...
- [SoapUI] 通过Groovy调用批处理文件.bat
import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def proje ...
- Ubuntu14.04-LTS 从系统安装到配置可用
1.安装Ubuntu14.04LTS-64bit 使用U盘安装很方便快捷,可以使用老毛桃使用iso模式制作一个U盘启动盘,然后分区安装. 如果使用硬盘安装的话需要注意的问题是: 如果电脑上以前有Lin ...
- BBS项目(2)
我们实现登录功能的随机验证码的产生 views.py def get_random_color(): return ( # 创建三个0-255的随机数 random.randint(0, 255), ...
- javascript总结39: 元素获取的常见问题
1 定义id属性的元素,不获取直接使用 由于id名具有唯一性,部分浏览器支持直接使用id名访问元素,但不是标准方式,生产环境下不推荐使用. 2 元素是对象 获取到的元素是DOM对象 ,DOM对象也有数 ...
- STS 闪退
# # A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ILLEGAL_INSTRUCTI ...
- 移动开发iOS&Android对比学习--异步处理
在移动开发里很多时候需要用到异步处理.Android的主线程如果等待超过一定时间的时候直接出现ANR(对不熟悉Android的朋友这里需要解释一下什么叫ANR.ANR就是Application Not ...
- 9.29学习的js基础
js基础 1.三种js引入方式 a).<input type="button" value="点击事件" onClick="documen ...