Dungeon Master (BFS与DFS的应用)
个人心得:一开始用DFS弄了半天一直输出不了结果,后面发现并没有进行判断;好不容易能够得出答案,结果超时了,才发现原来要用BFS;
对于DFS:
从一个点开始模拟能走的所有步骤,注意边界条件,走到不能走时返回上一步继续循环;耗时比较大,主要要注意当前的动作
格式的话
void dfs(int step)
{
判断边界
尝试每一种可能
{
注意标记;
继续下一步;
}
返回
}
BFS广度搜索,能够走得位置都走,将能到达的位置进入队列,当一个位置的所有动作完成时出队列,注意标志不改变,当队列为空时搜索完毕,当然找最小步伐时第一次到达时便是最小步骤!
struct Node
{
int x;//横坐标
int y;//纵坐标
int f;//纪录当下的编号,有时输出路径
int sum;//总步数
};
将开始的位置放入队列,sum=0,每一种可能判断,若能走标志并且进入队列,此时sum+1;一个位置的所有动作完成后,队列head出列;若查找到,退出,此时的总步数为queue【tail-1】.sum;
放题
Is an escape possible? If yes, how long will it take?
Input
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.
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!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct Node
{
int x,y,z;
int sum; };
int L,R,C;
char mapa[][][];
int book[][][];
void bfs(int a,int b,int c)
{
int next[][]={,,,-,,,,,,,-,,,,,,,-};
queue<Node> s;
Node n;
n.x=a,n.y=b,n.z=c,n.sum=;
book[a][b][c]=;
s.push(n);
while(!s.empty())
{ for(int i=;i<;i++)
{
Node m=s.front();
Node kk;
int t1=kk.x=m.x+next[i][];
int t2=kk.y=m.y+next[i][];
int t3=kk.z=m.z+next[i][];
kk.sum=m.sum+;
if(t1<||t2<||t3<||t1>L||t2>R||t3>C)
continue;
if(mapa[t1][t2][t3]=='E')
{
cout<<"Escaped in "<<kk.sum<<" minute(s)."<<endl;
return ; }
if(mapa[t1][t2][t3]=='.'&&book[t1][t2][t3]==)
{ s.push(kk);
book[t1][t2][t3]=; } }
s.pop(); }
cout<<"Trapped!"<<endl;
}
int main()
{ while(cin>>L>>R>>C)
{
int a,b,c;
if(!L&&!R&&!C)
break;
for(int i=;i<=L;i++)
for(int j=;j<=R;j++)
for(int k=;k<=C;k++)
{
cin>>mapa[i][j][k];
if(mapa[i][j][k]=='S') {a=i,b=j,c=k;} }
memset(book,,sizeof(book));
bfs(a,b,c); } return ; }
Dungeon Master (BFS与DFS的应用)的更多相关文章
- Dungeon Master poj 2251 dfs
Language: Default Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16855 ...
- hdu 2251 Dungeon Master bfs
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17555 Accepted: 6835 D ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- Dungeon Master bfs
time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- poj 2251 Dungeon Master (BFS 三维)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- [poj] 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( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- POJ2251 Dungeon Master(bfs)
题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
随机推荐
- Fidder详解之抓包
前言 本文是博主发表的第一篇文章,如有傻逼之处,请大家见谅.最近遇到很多人说接口相关的问题,比如:什么是接口,我该怎么做接口测试,还有我总是抓不到APP上的https请求(这个巨坑,不知道坑了多少小白 ...
- Spring session共享(使用redis)
SpringBoot+Redis实现HttpSession共享 前提:需要使用redis做session存储 一.效果演练(这里使用SpringBoot工程,Spring同理) 1.一个工程使用两个端 ...
- linux命令详解之df(6/19)
df命令作用是列出文件系统的整体磁盘空间使用情况.可以用来查看磁盘已被使用多少空间和还剩余多少空间. df命令显示系统中包含每个文件名参数的磁盘使用情况,如果没有文件名参数,则显示所有当前已挂载文件系 ...
- MIPI协议中文详解【转】
本文转载自:http://www.voidcn.com/blog/michaelcao1980/article/p-6254588.html 一.MIPI MIPI(移动行业处理器接口)是Mobile ...
- Java Junit5 Annotations
@BeforeEach 在方法上注解,在每个测试方法运行之前执行 @AfterEach 在方法上注解,在每个测试方法运行之后执行 @BeforeAll 该注解方法会在所有测试方法之前运行,该方法必须是 ...
- RPC与HTTP
一.为什么需要RPC,而不是简单的HTTP接口? RPC(即Remote Procedure Call,远程过程调用),主要是基于TCP/IP协议:而HTTP服务主要是基于HTTP协议的.我们都知道H ...
- numpy加权平均
import numpy as np a = np.arange(15).reshape(3,5) a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], ...
- RPM和yum相关
写在前面: 在这里可以知道rpm和yum的基本用法,找到更新本地yum源.搭建yum源的方法以及yum很琐碎的东西,包括yum源的优先级.用yum来安装或卸载CentOS图形界面包以及保存yum下载的 ...
- js中出现问题--Type Syntax error on token "catch", Identifier expected jquery.js
解决方案: 1.选中jQuery报错的web工程: 2.右键-->Myeclipse-->Exclude From Validation,选中: 3.继续右键Myeclipse--> ...
- Androidstudio与unity交互踩坑(黑屏问题)
unity打包好的apk运行出现黑屏,made with unity的界面都没有出现 原因是android项目中MainActivity没有继承UnityPlayerActivity