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 ...
随机推荐
- 前端css框架SASS使用教程(转)
一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一 ...
- iOS开发中常用的设计模式
常用的设计模式(一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过 ...
- 【搬砖】安卓入门(3)- Java开发编程基础--循环控制语句
04.01_Java语言基础(循环结构概述和for语句的格式及其使用) A:循环结构的分类 for(初始化表达式;条件表达式;循环后的操作表达式) { 循环体; } 复制代码 B:循环结构for语句的 ...
- Git从码云Clone代码到本地
Git从码云或者Github 克隆代码到本地 1.下载安装Git,傻瓜式下一步下一步即可... 2.配置Git: 2.1.选择你要clone到本地的路径:右键--->$ Git Bash Her ...
- rsync同步
本地同步: rsync -avz /boot /test C/S架构: 远程同步:rsync+ssh 远程浏览器目录文件:rsync 用户@192.168.0.250:/boot 下行:rsync ...
- Shell命令和流程控制
Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...
- 【Unity Shaders】学习笔记
http://www.cnblogs.com/-867259206/p/5596698.html
- ASP.NET之纠错
甘特图是:计划项目的时间安排和资源平衡 IoC容器会自动判断javabean和某些数据类型是否建立起依赖关系(装配是否成功),主要用在自动装配中. 通过设置属性<bean id="&q ...
- Hbase学习连接-数据导入
http://www.csdn.net/article/2014-01-07/2818046
- dll导入导出宏定义,出现“不允许 dllimport 函数 的定义”的问题分析
建立dll项目后,在头文件中,定义API宏 #ifndef API_S_H #define API_S_H ...... #ifndef DLL_S_20160424 #define API _dec ...