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 ...
随机推荐
- 【php】php操作MySQL数据库
一.操作步骤: 1. 连接MySQL数据库并判断是否连接成功2. 选择数据库3. 设置字符集4. 准备SQL语句5. 向MySQL服务发送SQL语句6. 解析处理结果集7. 释放结果集,关闭数据库连接 ...
- javaweb添加学生信息
连接数据库已经进行判断 要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分 ...
- Linux网络安全篇,FTP服务器的架设
一.FTP简介 FTP基于TCP协议.而且FTP服务器使用了命令通道和数据流通道两个连接.两个连接都会分别进行三次握手.在命令通道中客户端会随机取一个大于1024的端口与FTP服务器的21端口建立连接 ...
- 这份Java Web必读书单,值得所有Java工程师一看!
点击蓝色"程序员书单"关注我哟 加个"星标",每天带你读好书! 经过了10多年的发展,Java Web从开发框架到社区都已经非常成熟,而目前市面上最流行的Jav ...
- 31.1 Exception 的method :getMessage()、 printStackTrace()
package day31_exception; import java.lang.Exception; /* * Throwable的常用方法: String getMessage() :原因 St ...
- MySQL REPLACE INTO 的使用
前段时间写游戏合服工具时出现过一个问题,源DB和目标DB角色表中主键全部都不相同,从源DB取出玩家数据再使用 replace into 写入目标DB中,结果总有几条数据插入时会导致目标DB中原有的角色 ...
- JUC——检视阅读
JUC--检视阅读 参考资料 JUC知识图参考 JUC框架学习顺序参考 J.U.C学习总结参考,简洁直观 易百并发编程,实践操作1,不推荐阅读,不及格 JUC文章,带例子讲解,可以学习2 Doug L ...
- SpringBoot 2.x 开发案例之前后端分离鉴权
前言 阅读本文需要一定的前后端开发基础,前后端分离已成为互联网项目开发的业界标准使用方式,通过Nginx代理+Tomcat的方式有效的进行解耦,并且前后端分离会为以后的大型分布式架构.弹性计算架构.微 ...
- Weblogic-SSRF 漏洞复现
0x01 环境搭建 我这里使用的是vulhub,它几乎包含了所有的漏洞环境.(建议安装在ubuntu上) 有需要的小伙伴来企鹅群自取. 安装好vulhub之后需要cd 到weblogic ssrf 目 ...
- getline()和get()的使用区别
一.getline和get()的使用区别: 首先这两个函数都读取下一行输入,直到到达换行符:但是getline()函数会丢弃换行符,而get()将换行符保留在输入序列中 二.getline()函数的使 ...