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+优先队列)的更多相关文章

  1. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  2. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

  3. 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 ...

  4. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  5. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  6. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  7. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  8. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  9. 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 ...

  10. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. P3371 单源最短路径【模板】 洛谷

    https://www.luogu.org/problem/show?pid=3371 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含 ...

  2. 2017多校Round3(hdu6056~hdu6066)

    补题进度:7/11 1001 待填坑 1002 待填坑 1003(set) 题意: 给定长度为n(n<=5e5)的数组(是n的一个排列)和一个整数k(k<=80),f[l,r]定义为区间[ ...

  3. Nginx配置upstream实现负载均衡及keepalived实现nginx高可用

    (原文链接:http://www.studyshare.cn/blog-front//blog/details/1159/0 ) 一.准备工作 1.准备两个项目,发布到不同的服务器上,此处使用2个虚拟 ...

  4. Java中的数字

    以下内容引用自http://wiki.jikexueyuan.com/project/java/numbers.html: 通常情况下,当处理数字时,使用原始数据类型,如byte,int,long,d ...

  5. CSS3 水波纹

    css3 动画设置水波纹,效果如下图: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  6. stl 之set图解

    使用set或multiset之前,必须增加头文件<set> Set.multiset都是集合类,区别在与set中不同意有反复元素,multiset中同意有反复元素. sets和multis ...

  7. Please enter a commit message to explain why this merge is necessary.

    Please enter a commit message to explain why this merge is necessary. 请输入提交消息来解释为什么这种合并是必要的 git 在pul ...

  8. mac下配置phonegap(cordova)5.1.1开发环境

    眼下最新的cordova的版本号是5.1.1,在mac下搭建开发环境过程例如以下: 1)首先安装NODEJS环境 进入官网: http://nodejs.org/ .眼下的版本号: v0.12.7 点 ...

  9. E. Dreamoon and Strings(Codeforces Round #272)

    E. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. linux系列之-—01 shell编程笔记

    一.特殊变量($0.$1.$2. $?. $# .$@. $*) shell编程中有一些特殊的变量可以使用.这些变量在脚本中可以作为全局变量来使用. 名称 说明 $0 脚本名称 $1-9 脚本执行时的 ...