DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单。

 #include<math.h>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1234 int n,m,step;
int dx[]={,,,-};
int dy[]={,-,,};
char mat[N][N];
int vis[N][N];
int a[][N]; void dfs(int x,int y,int t)
{
if(step!=-)return;
if(x<||x>n||y<||y>m)return;
if(mat[x][y]=='#'||vis[x][y])return;
if(mat[x][y]=='r')
{
step=t;
a[][t]=x;
a[][t]=y;
return ; }
vis[x][y]=;
// printf("(%d,%d)\n",x,y);
a[][t]=x;
a[][t]=y;
for(int i=;i<;i++)
dfs(x+dx[i],y+dy[i],t+);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int stx,sty;
step=-;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf(" %c",&mat[i][j]);
if(mat[i][j]=='a')
stx=i,sty=j;
}
dfs(stx,sty,);
if(step==-)puts("Poor ANGEL has to stay in the prison all his life.");
else
{
cout<<step<<endl;
puts("Path");
for(int i=;i<=step;i++)
printf("%d,%d\n",a[][i],a[][i]);
} }
return ;
} /* 7 8
#.#####.
#.a#..r.
#..#....
..#..#.#
#...##..
.#......
........
*/

BFS记录路径:我的方法是每一个点都保存一下它从哪个方向走过来的(也就是那个i),这样由最后一个点,就可以倒推出倒数第二个点,倒数第二个点再可以推出倒数第三个点,最后推到起点,再反过来,就是路径了。

 #include<math.h>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1234
struct point
{
int x,y,t,dir;
}st; int n,m,step;
int dx[]={,,,-};
int dy[]={,-,,};
char mat[N][N];
int vis[N][N];
int xx[N];
int yy[N];
int d[N][N]; int bfs()
{
queue<point>q;
q.push(st);vis[st.x][st.y]=;
while(!q.empty())
{
point cur=q.front();
q.pop();
for(int i=;i<;i++)
{
point next=cur;
next.x+=dx[i];next.y+=dy[i]; if(next.x<||next.x>n||next.y<||next.y>m)continue;
if(mat[next.x][next.y]=='#'||vis[next.x][next.y]==)continue; d[next.x][next.y]=i; if(mat[next.x][next.y]=='.')next.t=next.t+;
if(mat[next.x][next.y]=='r')
{
xx[]=next.x;
yy[]=next.y;
step=next.t+;
return step;
}
q.push(next);vis[next.x][next.y]=;
}
}
return -;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
step=;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf(" %c",&mat[i][j]);
if(mat[i][j]=='a')
st.x=i,st.y=j,st.t=;
}
int ans=bfs();
if(ans==-)puts("Poor ANGEL has to stay in the prison all his life.");
else
{
cout<<ans<<endl; for(int i=;i<=step;i++)
{
xx[i]=xx[i-] - dx[ d[ xx[i-] ][ yy[i-] ] ];
yy[i]=yy[i-] - dy[ d[ xx[i-] ][ yy[i-] ] ];
}
puts("Path");
for(int i=step;i>=;i--)
{
printf("%d,%d\n",xx[i],yy[i]);
}
} }
return ;
} /* 7 8
#.#####.
#.a#..r.
#..#....
..#..#.#
#...##..
.#......
........ */

BFS和DFS记录路径的更多相关文章

  1. 哈密顿绕行世界问题(dfs+记录路径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...

  2. 迷宫问题 (bfs广度优先搜索记录路径)

    问题描述: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...

  3. 历届试题 危险系数-(dfs+记录路径)

     历届试题 危险系数   问题描述 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我 ...

  4. PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)

    1018 Public Bike Management (30 分)   There is a public bike service in Hangzhou City which provides ...

  5. Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)

    Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  6. bfs两种记录路径方法

    #include<cstdio> #include<queue> using namespace std; struct sss { int x,y; }ans[][]; ][ ...

  7. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  8. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  9. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

随机推荐

  1. Apache 流框架 Flink,Spark Streaming,Storm对比分析(1)

    此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.Flink架构及特性分析 Flink是个相当早的项目,开始于2008年,但只在最近才得到注意.Flink是 ...

  2. Codeforces Round #415 (Div. 2) 翻车啦

    A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. oracle11g rac 个性归档模式

    关闭数据库并将一节点启动到mount状态 [root@QSZAAS-XYORC02 ~]# su - grid[grid@QSZAAS-XYORC02 ~]$ srvctl stop database ...

  4. UITableView点击切换状态分析

    原理:多选+点击动画 初始化和点击都调用的方法 - (void)changeStateAnimated:(BOOL)animated { //不需要动画,初始化的时候 if (animated == ...

  5. 【luogu】P1772物流运输(最短路+DP)

    题目链接 对于本题我们设ext[i][j]计算第i个码头在前j天总共有几天不能用(其实就一前缀和),设dis[i][j]是从第i天到第j天不变运输路线的最短路径,设f[i]是前i天运输货物的最小花费. ...

  6. P1651 塔 (动态规划)

    题目描述 小明很喜欢摆积木,现在他正在玩的积木是由N个木块组成的,他想用这些木块搭出两座高度相同的塔,一座塔的高度是搭建它的所有木块的高度和,并且一座塔至少要用一个木块.每个木块只能用一次,也可以不用 ...

  7. ubuntu mysql安装及需要其他主机连服务器mysql时的设置(error:10061)

    说明: 一个朋友在使用ubuntu-server 16.04安装mysql,设置远程访问的时候出现了问题,请我帮忙.但是,我也没有使用过ubuntu安装mysql,于是乎搜索了很多技术文件,比着葫芦画 ...

  8. GridView选中,编辑,取消,删除代码

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 2.GridView选中,编辑,取消,删除: 效果图: 后台代码:你可以使用sqlhelper,本文没用。代码如下 ...

  9. ASP.NET里创建Microsoft Word文档

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 本文是应在ASP.NET里创建Microsoft Word文档之需而写的。这篇文章演示了在ASP.NET里怎么创建 ...

  10. dedecms--需要注意的细节

    在系统的系统配置参数里面修改一些参数 1:站点设置: (1):站点根网址:本地测试的话:就是你设置的虚拟主机:http://www.abc.cc (2):网页主页链接:为空 2:核心设置: DedeC ...