【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master
直接上中文了
Descriptions:
你被困在一个3D地牢中且继续寻找最短路径逃生!地牢由立方体单位构成,立方体单位中有的会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能向对角线的四个方向移动且迷宫四周环绕着许多岩石。
是否可以逃出地牢?如果可以,则需要多少时间?
Input
输入的第一行包含一个数,表示地牢的数量。
每个地牢的描述,其第一行包含三个数L,R和C(均小于等于30)。
L表示地牢的层数;R和C分别表示每层地牢的行与列的大小。
随后输入地牢的层数L,每层中包含R行,每行中包含C个字符。
每个字符表示地牢的一个单元。'#'表示岩石单元,'.'表示空白单元。你的起始位置在点'S',出口为'E'。
每层地牢的输入后都有一个空行。当L,R和C均为0时,输入结束。
Output
每个迷宫对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
题目链接:
https://vjudge.net/problem/POJ-2251
最短路bfs,和二维的基本一样,就是原来4个方向,现在6个方向,原来数组是二维,现在是三维,也相当于模板题了
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int sl,sx,sy;
int el,ex,ey;
char mp[][][];//记录地图
int vis[][][];//标记是否走过
int base[][] = { {-,,},{,,},{,-,},{,,},{,,-},{,,} };//六个方向
int l,r,c;
struct node
{
int f,x,y;//位置
int step;//步数
friend bool operator<(node a,node b)//步数小的现出来,即时间少
{
return a.step>b.step;//优先队列,步数小的先访问
}
};
priority_queue<node>q;
/*************************bfs***************************/
void bfs()
{
node p;
p.f=sl;//初始化
p.x=sx;
p.y=sy;
p.step=;
vis[sl][sx][sy]=;
q.push(p);
while(!q.empty())
{
node s=q.top();
q.pop();
if(s.f==el&&s.x==ex&&s.y==ey)//满足条件
{
printf("Escaped in %d minute(s).\n",s.step);
return;
}
for(int i=; i<; i++)//6种走法
{
int tl=s.f+base[i][];
int tx=s.x+base[i][];
int ty=s.y+base[i][];
if(mp[tl][tx][ty]!='#'&&tl>=&&tl<l&&tx>=&&tx<r&&ty>=&&ty<c&&!vis[tl][tx][ty])//判断是否能走
{
node e;
e.f=tl;
e.x=tx;
e.y=ty;
e.step=s.step+;
vis[e.f][e.x][e.y]=;
q.push(e);
}
}
}
cout<<"Trapped!"<<endl;
}
/**********************************主函数*********************************/
int main()
{
while(cin >> l >> r >>c,l+r+c)
{
for(int i=; i<l; i++)
{
for(int j=; j<r; j++)
{
cin >> mp[i][j];
for(int k=; k<c; k++)
{
if(mp[i][j][k]=='S')
{
sl=i;
sx=j;
sy=k;
}
if(mp[i][j][k]=='E')
{
el=i;
ex=j;
ey=k;
}
}
}
}
memset(vis,,sizeof(vis));//每次都要初始化
bfs();
}
}
【POJ - 2251】Dungeon Master (bfs+优先队列)的更多相关文章
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- poj 2251 Dungeon Master (BFS 三维)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- 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 ...
随机推荐
- (二)Commonjs规范与模块化
在之前的学习中我们使用require()来引入我们需要的包,这其实就是模块化,各模块相互独立,可以通过某种方式引入别的模块.而这些引入方式都是遵循一定的规范的,这就是CommonJS规范. 一.Com ...
- Codeforces 549C(博弈)
C. The Game Of Parity time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Help him--hdu5059(模拟 大坑)
http://acm.hdu.edu.cn/showproblem.php?pid=5059 直接说可能出现的情况 #include <iostream> #include <cst ...
- HDU 5360 【优先队列+贪心】
题意: 给定N个无序区间. 对合法区间的定义是: 在这个区间之前已经选出了至少l个合法区间,最多选出了r个合法区间.则该区间为合法区间. 输出最多能挑选出多少个合法区间,并输出合法区间的数量. 思路: ...
- pcre7.0在vc6.0编译
(0)从http://gnuwin32.sourceforge.net/packages/pcre.htm (pcre windows)下下载最新的windows平台源代码pcre-7.0-src. ...
- Ubuntu 16.04安装Guake Terminal终端(使用一键唤醒功能)
安装: sudo apt-get install guake-indicator sudo apt-get install guake 使用: 先启动guake-indicator,再启动guake. ...
- Ansible 详细用法说明(二)
setup:获取指定主机的facts. ===================================facts就是变量,内建变量 .每个主机的各种信息,cpu颗数.内存大小等.会存在fact ...
- hdu1042 (模拟n!)
题目大意: 求 n.(可能会超过整数范围,这里用数组模拟n!的值) pid=1042">http://acm.hdu.edu.cn/showproblem.php?pid=1042 A ...
- Zookeeper中的FastLeaderElection选举算法简述
Zookeeper是一个开源的分布式应用协调项目, 当中为了保证各节点的协同工作,Zookeeper在工作时须要有一个Leader. 而Leader是怎样被选举出来的?Zookeep中使用的缺省算法称 ...
- Android签名机制之---签名验证过程具体解释
一.前言 今天是元旦,也是Single Dog的嚎叫之日,仅仅能写博客来祛除寂寞了,今天我们继续来看一下Android中的签名机制的姊妹篇:Android中是怎样验证一个Apk的签名. 在前一篇文章中 ...