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 ...
随机推荐
- Atitit.软件兼容性原理与实践 v5 qa2.docx
Atitit.软件兼容性原理与实践 v5 qa2.docx 1. Keyword2 2. 提升兼容性的原则2 2.1. What 与how 分离2 2.2. 老人老办法,新人新办法,只新增,少修改 ...
- How To Join XLA_AE_HEADERS and RCV_TRANSACTIONS? [ID 558514.1]
Applies to: Oracle Inventory Management - Version: 12.0.6<max_ver> and later [Release: 12 an ...
- 移动端嵌入pdf.js远程请求pdf出现(206)
最近在做移动端的开发,需要嵌入pdf进行预览.看了很多的js组件后选择了pdf.js:使用起来还是比较方便的,至于使用网上有很多的教程. 但在使用过程中出现了如下一个问题(我做的是IOS系统): 问题 ...
- Android WebView 302斗争之旅
一.背景 越来越多的业务接入,项目内多多少少会出现几个H5页面,只是单纯的提供WebView容器接入H5页面根本满足不了需求,他们需要登录态,需要制定协议控制Native的导航栏,或者需要JsBrid ...
- Node.js 教程 02 - 经典的Hello World
前言: Node.js的介绍.安装及配置,上一节都已经介绍过了,如果有不清楚的也可以留言或者直接问度娘. 本节: 本节主要以一个简单的例子简单体验一下Node.js,用到了两种方法.下面会介绍. 总之 ...
- Oracle策略相关
Oracle策略可以限制查询.修改.删除.新增等操作,刚接触,对查询做一个测试: 参照 http://blog.csdn.net/diyyong/article/details/19552637 用法 ...
- PostgreSQL杀掉死锁的链接
查到对应的用户的活动连接: select * from pg_stat_activity where username="xxx"; 杀掉死锁的连接: select pg_term ...
- 机器学习实战笔记(Python实现)-08-线性回归
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- 统计分析中Type I Error与Type II Error的区别
统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...
- Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6
未能加载文件或程序集“Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6”或它的某一个 ...