1253 Dungeon Master
题目链接:
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 lineTrapped!
- 样例输入
-
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的更多相关文章
- 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 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- UVa532 Dungeon Master 三维迷宫
学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #i ...
- Dungeon Master poj 2251 dfs
Language: Default Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16855 ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- BFS POJ2251 Dungeon Master
B - Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- jpa命名规则 jpa使用sql语句 @Query
关键字方法命名sql where字句 AndfindByNameAndPwdwhere name= ? and pwd =? OrfindByNameOrSexwhere name= ? or sex ...
- Spark迷思
眼下在媒体上有非常大的关于Apache Spark框架的声音,渐渐的它成为了大数据领域的下一个大的东西. 证明这件事的最简单的方式就是看google的趋势图: 上图展示的过去两年Hadoop和Spar ...
- easyui combobox默认选中项
今天写前端代码发现combobox还挺难搞, $("#select_Dic").combobox({ url: "http: ...
- JavaScript中textRange对象使用方法总结
TextRange对象是动态HTML(DHTML)的高级特性,使用它可以实现很多和文本有关的任务,例如搜索和选择文本.文本范围让您可以选择性的将字符.单词和句子从文档中挑选出来.TextRange对象 ...
- JS获取当前项目名
代码如下: //获取当前网址,如: var curWwwPath=window.document.location.href; //获取主机地址之后的目录如:/Tmall/index.jsp var ...
- 根据日期切换图片KFX
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...
- js闭包的使用
js闭包的使用 学习了:https://www.cnblogs.com/ZinCode/p/5551907.html 终于用上了闭包,还是有些生涩:好像柿子还没熟: function createLi ...
- 不可不知的Python模块: collections
原文:http://www.zlovezl.cn/articles/collections-in-python/ Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想 ...
- ckeditor 4.2.1_演示 ckeditor 上传&插入图片
本文内容 FineUI ckeditor fckeditor/ckeditor 演示 ckeditor 4.2.1 上传&插入图片 最近看了一下 FineUI_v3.3.1 控件,对里边的 c ...