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 ...
随机推荐
- 重写NSString的setter方法
- (void)setName:(NSString *)name { _name = [name copy]; } 就可以了 不需要写成: 第一种: (void)setName:(NSString * ...
- Java Web学习总结(5)HttpServletRequest
一,HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- Python3解leetcode Factorial Trailing Zeroes
问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...
- Android工作两年之后的第一个App--天真无谐
一.前言 好长时间没写blog了,主要还是工作上的事有点多,周末又得在家开发自己的app,所以时间真的不够用了,当然今天这篇文章主要就要说一下,工作两年的我如何从产品角度去做一个app,以及app的发 ...
- linux0.11内核源码——用户级线程及内核级线程
参考资料:哈工大操作系统mooc 用户级线程 1.每个进程执行时会有一套自己的内存映射表,即我们所谓的资源,当执行多进程时切换要切换这套内存映射表,即所谓的资源切换 2.但是如果在这个进程中创建线程, ...
- flutter中的命名路由
命名路由是区别于基本路由的一种存在,方便于大型项目中路由的统一管理,现在,在前面基本路由的项目基础上实现实现命名路由. 使用步骤 路由配置 命名路由在使用前,需要在根组件main.dart中进行简单的 ...
- signer information does not match signer information of other classes in the same package
报错日志: java.lang.SecurityException: class "org.bouncycastle.asn1.ASN1ObjectIdentifier"'s si ...
- LOJ 3092 「BJOI2019」排兵布阵 ——DP
题目:https://loj.ac/problem/3092 同一个人的不同城堡之间没有什么联系,只是和<=m.所以对每个城堡的 s 个值排序,做一个 f[ i ][ j ] 表示第 i 个城堡 ...
- Buuctf | sqli-labs
这个是赵师傅给我们提供的训练靶场,最好都打一遍,但是出于找flag的角度,特此记录一下,flag在哪里[没错,我就是喜欢我的蓝变红,哈] ?id=1' :报错,说明就是用这个闭合的 ?id=0' un ...
- PyQuery爬取历史天气信息
1.准备工作: 网址:https://lishi.tianqi.com/xian/index.html 爬虫类库:PyQuery,requests 2.网页分析: 红线部分可更改为需要爬取的城市名,如 ...