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 ...
随机推荐
- IM协议
四种协议英文全称与简称 1->IMPP(Instant Messaging And PresenceProtocol):即时信息和空间协议 2->PRIM(Presence and Ins ...
- FTP下载
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import ja ...
- 常用的机器学习&数据挖掘知识点
Basis(基础):MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),MLE ...
- 【Topcoder】SRM157 DIV2总结
250分题:简单的二分,就是平常玩的猜数字游戏 代码:GitHub 500分题:给出一个员工一天的打卡时间段,要求求出员工这一天的工资.其中正常上班时间是6:00:00到18:00:00,薪水是wag ...
- collectionView的案例
#import "ViewController.h" #import "CollectionViewCell.h" @interface ViewControl ...
- Pytorch的gather用法理解
先放一张表,可以看成是二维数组 行(列)索引 索引0 索引1 索引2 索引3 索引0 0 1 2 3 索引1 4 5 6 7 索引2 8 9 10 11 索引3 12 13 14 15 看一下下面例子 ...
- Android开发 -- Bootloader
本文转载自:http://blog.csdn.net/jmq_0000/article/details/7378348 LK是什么 LK 是 Little Kernel 它是 appsbl (Appl ...
- Ubuntu 12.04下安装OpenCV 2.4.2
http://sourceforge.net/projects/opencvlibrary/files/ Ubuntu 12.04下安装OpenCV 2.4.2 http://blog.csdn.ne ...
- POJ 3159 最短路 SPFA
#include<iostream> using namespace std; const int nMax = 30005; const int mMax = 150005; const ...
- PAT1021. Deepest Root (25)
之前不知道怎么判断是不是树,参考了 http://blog.csdn.net/eli850934234/article/details/8926263 但是最后有一个测试点有超时,在bfs里我用了数组 ...