对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决。比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析。

常用步骤:


第一道题目:Dungeon Master  http://poj.org/problem?id=2251

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). 
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

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 line

Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0

此题目只要求了求出最短的路径步骤,肯定适合使用广度优先算法来处理,但是如果再要求输出对应最短路径下的具体路径,则只能使用深度优先来处理了。

先来一个广度优先算法的参考代码:

 #include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring> using namespace std; int l,r,c; struct node
{
int i;
int j;
int k;
int s;
}; string road[][];
int visited[**];//i*r*c+j*c+k为索引值 void getroad(int es)
{
int i,j,k;
node first,next;
bool flag=false; for(i=;i<l;i++)
{
for(j=;j<r;j++)
{
for(k=;k<c;k++)
{
if(road[i][j][k] == 'S')//找到初始点,标记
{
first.i=i;
first.j=j;
first.k=k;
first.s=;
flag=true;
break;
}
}
if(flag)
break;
}
if(flag)
break;
} queue<node> q;
q.push(first);
visited[first.i*r*c+first.j*c+first.k]=es; while(!q.empty())
{
first=q.front();
q.pop(); //需要设计上下左右前后6个方向,1维只有上下j左右k,2维以上再添加前后i
//同时需要考虑是否处在边界的情况
next=first;
next.s+=;
int index=; next.j-=;//上
index=next.i*r*c+next.j*c+next.k;
if(next.j>= && road[next.i][next.j][next.k] == '.')
{
if(visited[index] != es)
{
q.push(next);
visited[index]=es;
}
}
if(next.j>= && road[next.i][next.j][next.k] == 'E')
{
cout<<"Escaped in "<<next.s<<" minute(s)."<<endl;
return;
} next.j+=;//下
index=next.i*r*c+next.j*c+next.k;
//cout<<str[next.i][next.j][next.k]<<endl;
if(next.j<r && road[next.i][next.j][next.k] == '.')
{
if(visited[index] != es)
{
q.push(next);
visited[index]=es;
}
}
if(next.j<r && road[next.i][next.j][next.k] == 'E')
{
cout<<"Escaped in "<<next.s<<" minute(s)."<<endl;
return;
} next.j-=;//复原 next.k-=;//左
index=next.i*r*c+next.j*c+next.k;
if(next.k>= && road[next.i][next.j][next.k] == '.')
{
if(visited[index] != es)
{
q.push(next);
visited[index]=es;
}
}
if(next.k>= && road[next.i][next.j][next.k] == 'E')
{
cout<<"Escaped in "<<next.s<<" minute(s)."<<endl;
return;
} next.k+=;//右
index=next.i*r*c+next.j*c+next.k;
if(next.k<c && road[next.i][next.j][next.k] == '.')
{
if(visited[index] != es)
{
q.push(next);
visited[index]=es;
}
}
if(next.k<c && road[next.i][next.j][next.k] == 'E')
{
cout<<"Escaped in "<<next.s<<" minute(s)."<<endl;
return;
} next.k-=;//复原 if(l>)
{
next.i-=;//前
index=next.i*r*c+next.j*c+next.k;
if(next.i>= && road[next.i][next.j][next.k] == '.')
{
if(visited[index] != es)
{
q.push(next);
visited[index]=es;
}
}
if(next.i>= && road[next.i][next.j][next.k] == 'E')
{
cout<<"Escaped in "<<next.s<<" minute(s)."<<endl;
return;
} next.i+=;//后
index=next.i*r*c+next.j*c+next.k;
if(next.i<l && road[next.i][next.j][next.k] == '.')
{
if(visited[index] != es)
{
q.push(next);
visited[index]=es;
}
}
if(next.i<l && road[next.i][next.j][next.k] == 'E')
{
cout<<"Escaped in "<<next.s<<" minute(s)."<<endl;
return;
}
}
}
cout<<"Trapped!"<<endl;
} int main()
{
int count=; while(true)
{
cin>>l>>r>>c; if(l== && r== && c==)
break; int i=;
int j=;
string tmp;
for(i=;i<l;i++)
{
for(j=;j<r;j++)
cin>>road[i][j];
getline(cin,tmp);
}
getroad(count); for(i=;i<l;i++)
for(j=;j<r;j++)
road[i][j].clear(); count++;
} return ;
}

题目更改一下,要求输出最短步数,并且输出对应的具体路径。

考虑一下:

[ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)的更多相关文章

  1. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  2. 最近关于ACM训练与算法的总结

            到了大四以后越来越意识到基础知识的重要性,很多高屋建瓴的观点与想法都是建立在坚实的基础之上的, 招式只有在强劲的内力下才能发挥最大的作用,曾经有段时间我有这样的想法:我们出去以后和其他 ...

  3. 寒假的ACM训练(一)

    今天开始ACM训练,选择了刘汝佳的<挑战编程>,暂时算是开始了. 测评的网址: http://www.programming-challenges.com 第一个题目是水题啦.3n+1. ...

  4. 人工智能搜索算法(深度优先、迭代加深、一致代价、A*搜索)

    搜索算法问题求解 一.需求分析 分别用深度优先.迭代加深.一致代价.A*搜索算法得到从起始点Arad到目标点Bucharest的一条路径,即为罗马尼亚问题的一个解,在求解的过程中记录每种算法得到的解, ...

  5. 2014暑假ACM训练总结

    2014暑假ACM训练总结报告 匆匆之中,一个暑假又过去了,在学校训练的这段日子真的是感觉日子过得好快啊! 时光如箭,日月如梭! 匆忙的学习之中一个暑假就这样结束了,现在就来写一些总结吧,供自己以后阅 ...

  6. Java与算法之(5) - 老鼠走迷宫(深度优先算法)

    小老鼠走进了格子迷宫,如何能绕过猫并以最短的路线吃到奶酪呢? 注意只能上下左右移动,不能斜着移动. 在解决迷宫问题上,深度优先算法的思路是沿着一条路一直走,遇到障碍或走出边界再返回尝试别的路径. 首先 ...

  7. 广度优先算法(BFS)与深度优先算法(DFS)

    一.广度优先算法BFS(Breadth First Search) 基本实现思想 (1)顶点v入队列. (2)当队列非空时则继续执行,否则算法结束. (3)出队列取得队头顶点v: (4)查找顶点v的所 ...

  8. 必知必会JVM垃圾回收——对象搜索算法与回收算法

    垃圾回收(GC)是JVM的一大杀器,它使程序员可以更高效地专注于程序的开发设计,而不用过多地考虑对象的创建销毁等操作.但是这并不是说程序员不需要了解GC.GC只是Java编程中一项自动化工具,任何一个 ...

  9. 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)

    ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...

随机推荐

  1. jquery-fullpage-js制作页全屏滚动插件

    有一个很棒的插件 http://www.ijquery.cn/demo/fullPage/

  2. 1. K线基础知识一

    1. 什么是K线: K线起源于日本米市交易,它的基本用途就是为了寻找"买卖点". 2. K线按照计算周期可分为日K线,周K线,月K线,年K线. 周K线:周一的开盘价,周五的收盘价, ...

  3. 百度地图多点路径加载以及调整页面js

    $(document).ready(function () { /*用正则表达式获取url传递的地址参数,split后获得地址数组*/ bmap = new BMap.Map('mapcontaine ...

  4. react+redux官方实例TODO从最简单的入门(6)-- 完结

    通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...

  5. 微信的audio无法自动播放的问题

    一.问题 最近做了一个html5的项目,里面涉及到音乐播放,项目要求音乐进入页面就自动播放,于是我就想到了html5的audio标签,将mp3引入进去. 1.在audio标签里引入了autoplay属 ...

  6. Servlet技术(使用myeclipse)

    Servlet跟JavaBean本质上都是严格遵循规则的java包. Servlet基本结构: Public class Servlet 类名称 extends HttpServlet{     Pu ...

  7. SAP SMARTFORM 记录实际打印次数

    http://blog.csdn.net/wangjolly/article/details/8334008

  8. Reverse Core 第三部分 - 21章 - Windows消息钩取

    @author: dlive @date: 2016/12/19 0x01 SetWindowsHookEx() HHOOK SetWindowsHookEx( int idHook, //hook ...

  9. 自定义注解之运行时注解(RetentionPolicy.RUNTIME)

    对注解概念不了解的可以先看这个:Java注解基础概念总结 前面有提到注解按生命周期来划分可分为3类: 1.RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成clas ...

  10. 用typedef定义函数指针的问题

    在学习windows API的时候,遇到下面这段代码   以前见过的typedef的用法都是给一个数据类型取一个别名 typedef oldTypeName newTypeName   这种给数据类型 ...