POJ 2251 Dungeon Master(3D迷宫 bfs)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 28416 | Accepted: 11109 |
Description
Is an escape possible? If yes, how long will it take?
Input
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
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!
思路
迷宫问题简单变形,一个3D迷宫,给你起点终点,找出最短路径。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 35;
char maze[maxn][maxn][maxn];
int dis[maxn][maxn][maxn];
int L, R, C;
struct Node
{
int z, x, y;
Node(int zz, int xx, int yy): z(zz), x(xx), y(yy) {}
};
int bfs(int sz, int sx, int sy, int gz, int gx, int gy)
{
queue<Node>que;
int dx[5] = { -1, 0, 1,0}, dy[5] = {0, -1, 0, 1}, dz[3] = {0, -1, 1};
memset(dis, INF, sizeof(dis));
dis[sz][sx][sy] = 0;
que.push(Node(sz, sx, sy));
while (!que.empty())
{
Node pos = que.front();
que.pop();
if (pos.z == gz && pos.x == gx && pos.y == gy)
{
break;
}
for (int i = 0; i < 3; i++)
{
if (i)
{
int nz = pos.z + dz[i], nx = pos.x, ny = pos.y;
if (nz >= 0 && nz < L && nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nz][nx][ny] != '#' && dis[nz][nx][ny] == INF)
{
que.push(Node(nz, nx, ny));
dis[nz][nx][ny] = dis[pos.z][pos.x][pos.y] + 1;
}
}
else
{
for (int j = 0; j < 5; j++)
{
int nz = pos.z + dz[i], nx = pos.x + dx[j], ny = pos.y + dy[j];
if (nz >= 0 && nz < L && nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nz][nx][ny] != '#' && dis[nz][nx][ny] == INF)
{
que.push(Node(nz, nx, ny));
dis[nz][nx][ny] = dis[pos.z][pos.x][pos.y] + 1;
}
}
}
}
}
return dis[gz][gx][gy];
}
int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d%d%d", &L, &R, &C) && L && R && C)
{
int sz, sx, sy, gz, gx, gy;
for (int i = 0; i < L; i++)
{
for (int j = 0; j < R; j++)
{
scanf("%s", maze[i][j]);
for (int k = 0; k < C; k++)
{
if (maze[i][j][k] == 'S')
{
sz = i, sx = j, sy = k;
}
if (maze[i][j][k] == 'E')
{
gz = i, gx = j, gy = k;
}
}
}
getchar();
}
bfs(sz, sx, sy, gz, gx, gy) == INF ? printf("Trapped!\n") : printf("Escaped in %d minute(s).\n", dis[gz][gx][gy]);
}
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 ...
随机推荐
- file命令
命令简介: 该命令用来识别文件类型,也可用来辨别一些文件的编码格式.它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的. 执行权限 :All User 指令所在 ...
- Sharepoint 2010、Sharepoint 2013浏览器打开CAD(.dwg)
客户端配置 1.安装FreeDWGViewer.exe,设置浏览器查看 2.检查ActiveX插件是否已安装成功 服务端配置 1.开启许可模式或者通过脚本将"application/acad ...
- 2016总结Android面试题
1.简单的设计模式:单例模式:在系统中一个类只有一个实例. 分为懒汉模式和饿汉模式.饿汉模式的代码如下:public class Singleten{private static singleten ...
- Laravel大型项目系列教程(五)之文章和标签管理
一.前言 本节教程将大概完成文章和标签管理以及标签关联. 二.Let's go 1.文章管理 首先创建管理后台文章列表视图: $ php artisan generate:view admin.art ...
- 使用virtualenv搭建python3开发环境
问题描述 环境: CentOS6.5 想在此环境下使用python3进行开发,但CentOS6.5默认的python环境是2.6.6版本. 之前的做法是直接从源码安装python3,替换掉现有的开发环 ...
- iOS 浅复制和深复制的深层理解,含示例
转载:https://www.zybuluo.com/MicroCai/note/50592 版权归 @MicroCai 所有 以下是正文: 浅复制就是指针拷贝:深复制就是内容拷贝. 集合的浅复制 ( ...
- jquery validate表单验证插件-推荐
1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家. 1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素 3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...
- Python基本数据结构
第一部分: #列表a = [11,22,24,29,30,32] #1 把28插入到列表的末端 >>> a.append(28) >>> a [11, 22, 24 ...
- (原创)使用VMware安装Ubuntu,怎么无法使用startx进入桌面模式?
最近在VMware中安装Ubuntu时,发现VMware的快速安装后是文本模式,无法使用startx进入桌面模式,非常不方便.此问题为默认安装方式,需要设置安装方式. 操作系统:Windows 8.1 ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...