【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 ...
随机推荐
- CodeForces 597A Divisibility
水题. #include<iostream> #include<cstring> #include<cmath> #include<queue> #in ...
- [Bzoj4832][Lydsy2017年4月月赛]抵制克苏恩 (期望dp)
4832: [Lydsy2017年4月月赛]抵制克苏恩 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 673 Solved: 261[Submit][ ...
- hdu6200 mustedge mustedge mustedge (并查集+dfs序树状数组)
题意 给定一个n个点m条边无向图(n,m<=1e5) 支持两个操作 1.添加一条边 2.询问点u到点v的所有路径中必经边的条数 操作数<=1e5 分析 第一眼看起来像是要动态维护无向图的边 ...
- 转: java DES的算法片码
转自: https://www.zhihu.com/question/36767829 作者:郭无心链接:https://www.zhihu.com/question/36767829/answer/ ...
- Swift简单介绍 教程
Swift是什么? Swift是苹果于WWDC 2014公布的编程语言.这里引用The Swift Programming Language的原话: Swift is a new programmi ...
- 【Mongodb教程 第十一课 】MongoDB 聚合
聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...
- HDOJ 4455 Substrings 递推+树状数组
pre[i]第i位数往前走多少位碰到和它同样的数 dp[i]表示长度为i的子串,dp[i]能够由dp[i-1]加上从i到n的pre[i]>i-1的数减去最后一段长度为i-1的断中的不同的数得到. ...
- c语言学习-指针探究
1:指针定义格式:格式:变量类型 *变量名用途:指针变量用于储存地址(only),也就是根据地址值,访问对应的存储空间. 注意.int *p 只能指向int类型的数据: 例: int a = 20; ...
- java的Date日期使用
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...
- linux文件读写 文件锁、select、poll【转】
本文转载自:http://blog.csdn.net/fansongy/article/details/6853395 一.文件锁 文件锁用于多个用户共同使用或操作同一个文件.有读锁的时候可以再加读锁 ...