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 ...
随机推荐
- ASP.NET Core 中间件 中间件(Middleware)和过滤器(Filter)的区别
https://www.cnblogs.com/savorboard/p/5586229.html 前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际使用的, ...
- asp.net为什么会产生app_offline.htm 这个文件,为什么删除后运行浏览器就不会报应用程序脱机
一般是发布的时候自动生成的.VS2008在发布程序的时候,会首先在网站目录中生成这个文件,并把该虚拟目录的首页设成这个文件. 这样你在发布程序的时候如果有人访问网站就会看到这个页面. 不影响发布.ap ...
- XML和JSON优缺点
<1>.XML的优点 A.格式统一,符合标准: B.容易与其他系统进行远程交互,数据共享比较方便.<2>.XML的缺点 A.XML文件庞大,文件格式复杂,传输占带宽: B.服务 ...
- {{jQuery源码分析}}jQuery对象初始化的多种传参数形式
jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常 ...
- .NET MVC-去掉验证
将@Html.ValidationMessage("sortid")代码去掉 将if (ModelState.IsValid)验证代码去掉
- c#:对两个字符串大小比较(不使用c#/java内部的比较函数),按升序排序
题目:首先需要实现一个函数:两个字符串大小比较(不得使用c#/java系统函数)的自定义函数:之后对一个字符串数据进行按升序排序(在排序过程中使用字符串大小比较时,使用自定义的字符串大小比较函数). ...
- 4、Cocos2dx 3.0游戏开发找小三之Hello World 分析
尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27186557 Hello World 分析 打开新 ...
- CareerCup Facebook Total number of substring palindrome
Write a function for retrieving the total number of substring palindromes. For example the input is ...
- FireDAC中的SQLite(二)
我们接下来将要使用FDDemo.sdb数据库进行访问,开始我们的第一个SQLite访问例子. 我们的FDDemo.sdb存放目录在:C:\Program Files (x86)\Embarcadero ...
- artTemplate 原生 js 模板语法版
在页面中引用模板引擎: <script src="dist/template-native.js"></script> 下载 表达式 <% 与 %&g ...