POJ 2251 Dungeon Master(dfs)
Description
Is an escape possible? If yes, how long will it take?
Input
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).
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.
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'.
terminated by three zeroes for L, R and C.
Output
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!
Sample Input
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
就是一个人被逮到地牢里面了,地牢有好几层(L),每层R*C,标#的地方不能走,每次可以上、下、左、右、前、后走一个相邻的格子,花费1分钟。问你从S走,能不能逃出这个地牢(走到E)。
这是我认真的写的第一个dfs,是不是有点晚呢?呵呵。总结一下dfs的几个要点吧。 1.struct node 用于标记点的坐标,和到达(如果能)该店的步数。
2.vis dir 数组,分别表示 是否访问过 每次走的方向
3.check函数 :检查3点 1->是否超出边界 2->是否走过 3->是否能走
4.基于队列实现的bfs函数,这里详细解释
首先我们建立一个node的队列q,我们先把起点加进队列。从当q的队首开始,用temp取得这个队首,q.pop().将通过temp能走到的node再加入到队列里面,同时判断是否到达终点。
这样知道q这个队列空了,我们就走完了所有可能的路了。 代码如下:
#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;
struct node
{
int l,x,y;
int step;
};
int L,R,C;
int el,ex,ey,sl,sx,sy,ans;
char mp[][][];
bool vis[][][];
int dir[][]={,,, -,,, ,,, ,-,, ,,, ,,-};
bool check(node now)
{
if (now.l>=L||now.l<||
now.x>=R||now.x<||
now.y>C||now.y<)
return ;
if (mp[now.l][now.x][now.y]=='#'||vis[now.l][now.x][now.y])
return ;
return ;
}
void bfs (int level,int xx,int yy)
{
queue<node> q;
node now;
now.l=level,now.x=xx,now.y=yy,now.step=;
vis[level][xx][yy]=true;
q.push(now);
while (!q.empty())
{
node temp=q.front();
q.pop();
for (int i=;i<;++i)
{
now.l=temp.l+dir[i][];
now.x=temp.x+dir[i][];
now.y=temp.y+dir[i][];
now.step=temp.step+;
if (check(now))
{
if (mp[now.l][now.x][now.y]=='E')
{
ans=now.step;
return;
}
vis[now.l][now.x][now.y]=true;
q.push(now);
}
}
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d%d",&L,&R,&C))
{
if (L==&&R==&&C==)
break;
el=ex=ey=;
sl=sx=sy=;
memset(vis,false,sizeof vis);
for (int i=;i<L;++i)
{
for (int j=;j<R;++j)
{
for (int k=;k<C;++k)
{
cin>>mp[i][j][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;
}
}
}
}
ans=-;
bfs(sl,sx,sy);
if (ans!=-)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return ;
}
POJ 2251 Dungeon Master(dfs)的更多相关文章
- poj 2251 Dungeon Master(bfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- POJ 2251 Dungeon Master (非三维bfs)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55224 Accepted: 20493 ...
- POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48380 Accepted: 18252 ...
- POJ 2251 Dungeon Master(广搜,三维,简单)
题目 简单的3d广搜,做法类似与 hdu 的 胜利大逃亡 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<str ...
- POJ 2251 Dungeon Master(三维空间bfs)
题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue& ...
- POJ - 2251 Dungeon Master(搜索)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
随机推荐
- UITextfield 允许和禁止编辑
1.enabled属性 2.resignFirstResponder,设置的时候,如果不起作用,可以延时一会儿,因为键盘升起需要时间. dispatch_after(dispatch_time(DIS ...
- Codeforces 837D--Round Subset (DP)
原题链接:http://codeforces.com/contest/837/problem/D 题意:在n个数字中,取k个数,使这些数的乘积后缀“0”的个数最大,输出后缀0的最大数量. 思路:显然只 ...
- NAGIOS(网络监视工具)
Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机,路由器等网络设备,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员 ...
- 前端每日实战:19# 视频演示如何用纯 CSS 创作一种有削铁如泥感觉的菜单导航特效
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/XqYroe 可交互视频教程 此视频 ...
- JSON的android应用实例
JSON的android应用实例 Json在线解析器 下面是直接通过JUnit来测试直接通过API来解析Json数据 1.普通键值对象 2.Json数组对象 3.Json数组对象
- Linux 学习 (五) DNS配置
没有配置DNS会引起的问题 yum命令 ssh命令等不能进行 错误: Could not resolve host: centos.ustc.edu.cn; 本文例子: CentOS7 下DNS配置 ...
- JS:收集的一些Array及String原型对象的扩展实现代码
扩展Array的原型对象的方法 // 删除数组中数据 Array.prototype.del = function(n) { if (n<0) return this; return this ...
- php如何获取服务器端的一些信息
<?php echo "服务器端的IP地址是:<br />"; echo $_SERVER['SERVER_ADDR']; echo "服务器端的端口号 ...
- linux composer 安装与应用
linux下composer安装与简单应用-------------------------------------安装------------------------------------//下载 ...
- HBase 中加盐之后的表如何读取:Spark 篇
在 <HBase 中加盐之后的表如何读取:协处理器篇> 文章中介绍了使用协处理器来查询加盐之后的表,本文将介绍第二种方法来实现相同的功能. 我们知道,HBase 为我们提供了 hbase- ...