题目链接:

http://noi.openjudge.cn/ch0205/1253/

http://poj.org/problem?id=2251

总时间限制: 1000ms  内存限制: 65536kB
描述
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take? 

输入
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
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.
输出
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

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!

样例输入
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!

题目大意与算法分析:
这题是一个三维的迷宫题目,其中用‘.’表示空地,用‘#’表示障碍物,'S'表示出发点,
'E'表示终点,求从起点到终点的最小移动次数。
解法和二维类似,只是在行动时除了东南西北移动外还多了上下。可以上下左右前后移动,
每次都只能移动到相邻的空位,每次需要花费一分钟,求从起点到终点最少需要多久。

输入有若干组测试数据,每一组数据格式如下:
首先输入z,x,y表示三维迷宫的规模,有z层,每层是x行y列。 (z,x,y都在30以内.)
然后输入z层的数据,每一层是x*y的字符数组,行内字符之间无空格。
当输入的z,x,y都为0时表示输入结束。

对每一组测试数据,输出最少需要的时间,格式如"Escaped in 11 minute(s).",每组数据的结果占一行。
假如无法到达,输出"Trapped!".

这题用BFS解,每次去队首元素,如果是终点则输出结果移动的次数,否则,从该点开始分别
向东南西北上下移动(如果可以走的话)并继续搜,如果到队列为空还没搜到解法,则说明无解。

题解可以参考:
http://www.cnblogs.com/ACShiryu/archive/2011/07/23/2114994.html
http://blog.csdn.net/libin56842/article/details/23702395

 #include<stdio.h>
#include<iostream>
#include<queue>
using namespace std; struct obj
{
int zz,xx,yy;
int step;//到达该点的步数
}; int z,x,y;
char a[][][];
queue<struct obj> q;
struct obj start,End;
bool flag; int dz[]={,,,,,-};//上右下左前后
int dx[]={-,,,,,};
int dy[]={,,,-,,};
void BFS();//广搜,数据全部在全局变量 int main(int argc, char *argv[])
{
int i,j,k; while(scanf("%d%d%d",&z,&x,&y)!=EOF)
{
getchar();//吸收回车
if(z==&&x==&&y==) break; //输入三维迷宫
for(k=;k<z;k++)
{
for(i=;i<x;i++)
{
for(j=;j<y;j++)
{
a[k][i][j]= getchar();
if(a[k][i][j]=='E')
{
a[k][i][j]='.';
End.zz=k;
End.xx=i;
End.yy=j;
}
else if(a[k][i][j]=='S')
{
start.zz=k;
start.xx=i;
start.yy=j;
start.step=;
}
}
getchar();//吸收回车
}
getchar();//吸收回车
} flag=false;//尚未找到目的地
BFS();
if(flag==true)
printf("Escaped in %d minute(s).\n",End.step);
else printf("Trapped!\n");
}
return ;
} void BFS()//广搜,数据全部在全局变量
{
int i,tzz,txx,tyy;
struct obj temp; while(!q.empty()) q.pop();//清空队列 q.push(start);//出发点入队
while(!q.empty())
{
for(i=;i<;i++)
{
tzz=(q.front()).zz+dz[i];
txx=(q.front()).xx+dx[i];
tyy=(q.front()).yy+dy[i];
if(tzz>=&&tzz<z&&txx>=&&txx<x&&tyy>=&&tyy<y&&a[tzz][txx][tyy]=='.')
{
temp.zz=tzz;
temp.xx=txx;
temp.yy=tyy;
temp.step=(q.front()).step+;
a[tzz][txx][tyy]='#';
q.push(temp);
if(temp.zz==End.zz&&temp.xx==End.xx&&temp.yy==End.yy)
{
End.step=temp.step;
flag=true;
return ;
}
}
}
q.pop();
}
return ;
}

1253 Dungeon Master的更多相关文章

  1. NOI2.5 1253:Dungeon Master

    描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of ...

  2. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  3. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...

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

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

  6. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

  7. Dungeon Master poj 2251 dfs

    Language: Default Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16855 ...

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

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

  9. BFS POJ2251 Dungeon Master

    B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. idea 2018.1破解激活方法,有效期至2099年 idea 激活 破解

    最近笔者测试了好多破解Idea的方法,最简单操作方法莫过于用license server激活,但是此类方法对最新的2017.3.2版已经无效了,亲测哦,如下图所示.  针对新版的IntelliJ ID ...

  2. Java hashCode() 和 equals()的若干问题解答<转载自skywang12345>

    第1部分 equals() 的作用equals()的作用是用来判断两个对象是否相等.equals()定义在JDK的Object类中.通过判断两个对象的地址是否相等(即,是否是同一个对象)来区分它们是否 ...

  3. 我一直跑的分类LSTM模型原来是这一个,新闻分类网络

    原始的github可以参考这里: https://github.com/FudanNLP/nlpcc2017_news_headline_categorization 我的经验文章可以参考这里: ht ...

  4. jQuery中的编程范式

    浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...

  5. 利用blob对象实现粘贴图片

    blob的一个常用应用场景,就是获取剪切板上的数据来进行粘贴的操作.例如通过QQ截图后,需要在网页上进行粘贴操作. 粘贴图片我们需要解决下面几个问题 1.监听用户的粘贴操作 2.获取到剪切板上的数据 ...

  6. 使用 CSS 接收用户的点击事情并对相关节点进行操作

    问题背景:使用纯 CSS 方案,实现导航栏tab切换 实现 Tab 切换的难点在于如何使用 CSS 接收到用户的点击事情并对相关的节点进行操作.即是: 如何接收点击事件 如何操作相关DOM 下面看看如 ...

  7. 【Spark】开发Spark选择Java还是Scala?

    Spark-Java-Scala-哪种语言 spark java 支持_百度搜索 (1 封私信)Spark 中用 Scala 和 java 开发有什么区别? - 知乎 (1 封私信)Spark平台下, ...

  8. SCIKIT-LEARN与GBDT使用案例

    http://blog.csdn.net/superzrx/article/details/47073847 安装 SCIKIT-LEARN是一个基于Python/numpy/scipy的机器学习库  ...

  9. 什么是BFC(Block Formatting Context)

    原文:https://segmentfault.com/a/1190000012221820 https://www.w3.org/TR/CSS2/visuren.html#block-formatt ...

  10. Google声明机器学习在自己定制的芯片比方普通的GPU和CPU快15到30倍

    GOOGLE开发自己的加速机器学习的芯片已经不是什么秘密了,最先发布出来的是TPU(Tensor Processing Units),在2016年5月I/O开发大会上发布的.可是没有发布相关的细节情况 ...