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 ...
随机推荐
- codeblocks 的安装与初体验
下载 链接 安装 点击默认安装即可! 配置 自带编译器还报错!! 大多数自带编译器的codeblocks安装后无法找到编译器的解决方案 点击Settings->Compiler,找到Toolc ...
- Blocked Billboard II题解--模拟到崩溃的模拟
前言 比赛真的状态不好(腐了一小会),导致差点爆0. 这个题解真的是在非常非常专注下写出来的,要不然真的心态崩. 题目 题目描述 奶牛Bassie想要覆盖一大块广告牌,她在之前已经覆盖了一小部分广告牌 ...
- mpvue微信小程序怎么写轮播图,和官方微信代码的差别
目前用mpvue很多第三方的ui库是引入不了的,因为它不支持含有dom操作. 那我们要做轮播图的话一个是手写另外一个就是用小程序的swiper组件了: 官方代码: <swiper indicat ...
- AntSword 中国蚁剑的下载安装配置(附下载文件)
文章更新于:2020-04-11 按照惯例,需要的文件附上链接放在文首. 文件一: antSword-2.1.8.1.zip.7z 文件大小: 14.3 MB 下载链接: 中国蚁剑 v2.1.8.1 ...
- Vue的基本指令的使用1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 7L-双线链表实现
链表是基本的数据结构,尤其双向链表在应用中最为常见,LinkedList 就实现了双向链表.今天我们一起手写一个双向链表. 文中涉及的代码可访问 GitHub:https://github.com/U ...
- Android 添加键值并上报从驱动到上层
转载:https://blog.csdn.net/weixin_43854010/article/details/94390803 Android 添加键值并上报从驱动到上层 平台 :RK3288 O ...
- Scala函数式编程(六) 懒加载与Stream
前情提要 Scala函数式编程指南(一) 函数式思想介绍 scala函数式编程(二) scala基础语法介绍 Scala函数式编程(三) scala集合和函数 Scala函数式编程(四)函数式的数据结 ...
- WEB页面实现方法
页面分类 :添加页.修改页.列表页.详情页.功能页.删除 一.添加 1) 准备tpl.action(添加页.添加页保存公用一个action),并确认是否登录才显示2) 书写添加页action代码,例如 ...
- 29 collection 集合体系结构
/*collection:采集 * ArrayList * 集合的体系结构: * 由于不同的数据结构(数据的组织,存储方式),所以Java为我们提供了不同的集合, * 但是不同的集合他们的功能都是相似 ...