bfs—Dungeon Master—poj2251
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 32228 | Accepted: 12378 |
http://poj.org/problem?id=2251
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! 普通的bfs迷宫题,只是增加了第3维。
#include<iostream>
#include<queue>
#include<cstring>
using namespace std; const int MAX=;
struct Node
{
char c;
int pace,z,x,y;//z,x,y--楼层,行,列
}maze[MAX][MAX][MAX];//存储迷宫字符,坐标,到达步数
int L,R,C;
int dre[][]={{,,},{-,,},{,,},{,-,},{,,},{,,-}};//方向:东南西北上下
bool check(int z,int x,int y)//检查坐标是否超出范围
{
return (z>=&&z<=L&&x>=&&x<=R&&y>=&&y<=C);
}
int bfs(int z0,int x0,int y0)//找到从点[z0][x0][y0]到终点的最小步数
{
queue<Node> Q;
Node t;
Q.push(maze[z0][x0][y0]);//放入起点
while(!Q.empty())
{
t=Q.front();
Q.pop();
if(t.c=='E') return t.pace;//找到,返回步数
for(int i=;i<;i++)//枚举6个方向,若可以走且未走过就push到队尾
{
int Z=t.z+dre[i][],X=t.x+dre[i][],Y=t.y+dre[i][];
if(check(Z,X,Y))
{
if(maze[Z][X][Y].c!='#'&&!maze[Z][X][Y].pace)
{
maze[Z][X][Y].pace=t.pace+;//步数等于上一步(t)的步数加1
Q.push(maze[Z][X][Y]);
}
}
}
}
return -;//找不到返回-1
}
int main()
{
while(cin>>L>>R>>C,L||R||C)
{
int sz,sx,sy;
memset(maze,,sizeof(maze));
for(int i=; i<L; i++)
for(int j=; j<R; j++)
for(int k=; k<C; k++)
{
cin>>maze[i][j][k].c;
maze[i][j][k].z=i;
maze[i][j][k].x=j;
maze[i][j][k].y=k;
if(maze[i][j][k].c=='S')//记录起点
{
sz=i;
sx=j;
sy=k;
}
}
int re=bfs(sz,sx,sy);
if(re!=-) cout<<"Escaped in "<<re<<" minute(s).\n";
else cout<<"Trapped!\n";
}
return ;
}
bfs—Dungeon Master—poj2251的更多相关文章
- Dungeon Master POJ-2251 三维BFS
题目链接:http://poj.org/problem?id=2251 题目大意 你被困在了一个三维的迷宫,找出能通往出口的最短时间.如果走不到出口,输出被困. 思路 由于要找最短路径,其实就是BFS ...
- BFS POJ2251 Dungeon Master
B - Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (非三维bfs)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55224 Accepted: 20493 ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48380 Accepted: 18252 ...
随机推荐
- 数据科学 R语言速成
文章更新于:2020-03-07 按照惯例,需要的文件附上链接放在文首: 文件名:R-3.6.2-win.exe 文件大小:82.4M 下载链接:https://www.lanzous.com/i9c ...
- JAVA自动化之Junit单元测试框架详解
一.JUnit概述&配置 1.Junit是什么? Junit是一个Java 编程语言的开源测试框架,用于编写和运行测试.官网 地址:https://junit.org/junit4/ 2.Ma ...
- Matlab入门(二)
数据类型 1.整形 有符号 1 字节整数 -27 - 27-1 int8() 有符号 2 字节整数 -215 - 215-1 int16() 有符号 4 字节整数 -231 - 231-1 int32 ...
- iOS岗位招聘标准水涨船高,五年iOS程序员表示面试太难了
人才济济的iOS开发者,你凭什么脱颖而出? 与岗位要求相去甚远,如何挑战极限? 想去心怡公司,如何马到成功? 那么,你的绝招是什么呢? 在这个iOS岗位供不应求的市场,对iOS开发者对要求日益增长,面 ...
- 34.1 字符流-- FileRead FileWrite
一次读取一个字符 FileReader fr = new FileReader("aa.txt"); // System.out.println(fr.read()); // Sy ...
- 会话技术(session/cookie)
session可保存int double bool array string object:cookie只能保存stringsession 可通过php.ini文件查看存放的位置:cookie不同浏览 ...
- python3(八) function
# Python 常用内置函数 https://docs.python.org/3/library/functions.html#abs print(help(abs)) # Return the a ...
- Git中rebase失败了如何进行恢复
rebase失败后的恢复 记一次翻车现场 记一次翻车的现场,很早之前提的PR后面由于需求的变便去忙别的事情了,等到要做这个需求的我时候,发现已经 落后版本了,并且有很多文件的冲突,然后就用rebase ...
- std::forward和std::move
std::forward完美转发 保证参数原来的属性(用在template的参数是引用的时候):左值引用在被转发之后仍然保持左值属性,右值引用在被转发之后依然保持右值属性 void show(int& ...
- python 3 的解释器
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:Yangtze PS:如有需要Python学习资料的小伙伴可以加点击下 ...