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. python红包随机生成(隔板法)

    #红包生成思路#200 块钱 10个红包#0-200 的一个轴,随机取9个点,分成10段, 每一段的值表示一个红包的大小 #把输入的 money值 * 100 拿到的数值就是分, 不用再考虑单位是元的 ...

  2. 17,时间模块 time,random模块

    表示时间的三种方式 在python中,通常有着三种方式来表示时间:时间戳,元祖,格式化的时间字符串: 1,时间戳(timestamp):通常来说时间戳表示的是从1970年1月1日00:00:00开始按 ...

  3. 【14】PNG,GIF,JPG的区别及如何选

    [14]PNG,GIF,JPG的区别及如何选 GIF: 8位像素,256色 无损压缩 支持简单动画 支持boolean透明 适合简单动画 JPEG: 颜色限于256 有损压缩 可控制压缩质量 不支持透 ...

  4. Title共通写法

    用: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_c ...

  5. 【Luogu】P1941飞扬的小鸟(DP)

    我发现现在没了题解我做普及提高+的题也做不了 更不要说这些提高+难度的‍题 此题是一个二维DP.暴力是三重循环ijk,k枚举在i位置上的点击次数.即 for(int i=1;i<=n;++i) ...

  6. HDU 1071 The area ——微积分

    [题目分析] 求二次函数和一次函数围成的面积. 先解方程求出一次函数和二次函数. 然后积分. 现在还是不会积分. [代码] #include <cstdio> #include <c ...

  7. [luoguP3413] SAC#1 - 萌数(数位DP)

    传送门 gtm的数位dp! 看到好多题解,都是记忆化搜索,好像非常方便啊,但是我还是用递推好了,毕竟还是有些类似数位dp的题用递推的思路,记忆化做不了,现在多培养一下思路 首先这道题, 只看长度大于等 ...

  8. hdu 2713

    #include<stdio.h> #include<string.h> int map[151000][2]; int max(int a,int b) {  return ...

  9. 关于虚拟机IP网段和公司内网网段的问题?

    开发四年只会写业务代码,分布式高并发都不会还做程序员?->>>    请教一个问题,为了解决电脑换网络环境就连不上虚拟机的问题,我虚拟机使用的nat模式,我的VMnet8IP是192 ...

  10. 【POJ1226】Substrings(后缀数组,二分)

    题意: n<=10,len<=100 思路: 只有一个字符串的时候特判一下 #include<cstdio> #include<cstring> #include& ...