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. mac下secureCRT 客户端 $redis-cli回车后没有反应的解决办法

    启动redis server后,SecureCRT进入redis-cli,输入不断在后面追加IP:Port显示设置当前的Session Options-->Terminal-->Emula ...

  2. vue.js+element-ui

    git地址:https://github.com/jerry9022/LitAdmin vue.js+element-ui 做后台管理系统 太方便了

  3. charles-修改发送的接口数据测试页面样式

    一.痛点: 1.    界面上数据准确性无法比对 2.     界面上几乎没有可测试数据 3. 消息条数超过99时的显示逻辑验证(难道真的要造100条新的未读消息?) 4. 更换界面图片时必须找相关接 ...

  4. Visual Studio 2013 滚动条实现代码缩略图

    启动Visual studio 2013,打开工具->选项   在搜索选项输入,滚动条,英文版大概输入Scroll bar or Scroll 或者:文本编辑器->所有语言->滚动条 ...

  5. 九度oj 题目1014:排名

    题目描述:     今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后通过分数线的考生,并将他 ...

  6. 【bzoj4408】[Fjoi 2016]神秘数 主席树

    题目描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1+1+14 = 45 = 4+16 = 4+1+1 ...

  7. Codeforces Round #354 (Div. 2)——C. Vasya and String(尺取)

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. 算法复习——floyd求最小环(poj1734)

    题目: 题目描述 N 个景区,任意两个景区之间有一条或多条双向的路来连接,现在 Mr.Zeng 想找一条旅游路线,这个路线从A点出发并且最后回到 A 点,假设经过的路线为 V1,V2,....VK,V ...

  9. Ubuntu 常用命令和一些 tips

    001. ubuntu 解压.tar.xz文件到另一个文件夹:sudo tar -xvJf ***.tar.xz -C /usr/src sudo 超级用户tar [选项...][file]...-x ...

  10. centos7如何查看ip信息(centos 6.5以前都可以用ifconfig 但是centos 7里面没有了,centos 7用什么查看?)

    展开全部 centos7如何查看ip信息可以这样解决: 1.首先要先查看一下虚拟机的ip地址,因为ipconfig不是centos7,因此要使用 ip addr来查看. 2.查看之后你就会发现ens3 ...