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 ...
随机推荐
- [Link]Gearman分布式任务处理系统
http://blog.csdn.net/jiao_fuyou/article/category/1745977 http://www.cnblogs.com/cocowool/archive/201 ...
- Zookeeper Tutorial 1 -- Overview
ZooKepper: 一个分布式应用的分布式协调服务(Distributed Coordination Service) 分布式服务难以管理, 他们容易造成死锁和竞争, ZooKepper的动机就是为 ...
- ASP.NET MVC:UrlHelper.cs
ylbtech-funcation-Utility: ASP.NET MVC:UrlHelper.cs 充当表示 ASP.NET Razor 页的类的基类. 1.UrlHelper 类返回顶部 1-1 ...
- 检测Sql Server服务器SQL语句执行情况
1.查找目前SQL Server所执行的SQL语法,并展示资源情况: SQL code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- 相声段子:How Are You
/**************************************************************** File name : HowAreYou Author : 叶飞影 ...
- 漂亮的CSS3提交意见输入框样式
做了个输入框样式,如图: CSS代码如下: <喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" cl ...
- 手动建库时一个错误:Error accessing PRODUCT_USER_PROFILE
首先感谢前辈的文章:http://space.itpub.net/14184018/viewspace-691502 手动创建数据库完成后,创建新用户,并用新用户登陆到数据库时,报错如下: SYS@ ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- 如何实现Linux+Windows双系统启动
设置你的计算机根据需要启动 Windows 10 或 Ubuntu 18.04. 尽管 Linux 是一个有着广泛的硬件和软件支持的操作系统,但事实上有时你仍需要使用 Windows,也许是因为有些不 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十一)NIFI1.7.1安装
一.nifi基本配置 1. 修改各节点主机名,修改/etc/hosts文件内容. 192.168.0.120 master 192.168.0.121 slave1 192.168.0.122 sla ...