BFS和DFS记录路径
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记录路径的更多相关文章
- 哈密顿绕行世界问题(dfs+记录路径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...
- 迷宫问题 (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, ...
- 历届试题 危险系数-(dfs+记录路径)
历届试题 危险系数 问题描述 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我 ...
- 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 ...
- 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, ...
- bfs两种记录路径方法
#include<cstdio> #include<queue> using namespace std; struct sss { int x,y; }ans[][]; ][ ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 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 ...
随机推荐
- mysql复制延迟排查
今天收到报警,提示从库延时,首先当然是上去查看情况,首先查看机器负载,如下: 可以看到使用cpu已经100%,io没有等待.那么查看mysql是什么情况,执行show processlist没有发现任 ...
- docker+Battery Historian 环境搭建(电量分析)
docker 安装(windows) 1. 下载 https://docs.docker.com/docker-for-windows/install/ 和 安装和添加环境变量(...) 2. 安 ...
- GT使用说明
GT文档:https://gt.qq.com/docs.html GT Android版的详细使用手册: https://gt.qq.com/docs/a/GTAndroidUserGuide.pdf
- 汇编CMOS
汇编 端口 端口 前面讲过,各种存储器都和CPU的地址线.数据线.控制线相连.CPU在操控它们的时候,把它们都当作内存来对待,把它们总的看做一个由若干存储单元组成的逻辑存储器,这个逻辑器我们称其为内存 ...
- pytorch遇到的问题:RuntimeError: randperm is only implemented for CPU
由此,我们找到sample.py,第51行如下图修改
- SPOJ - Distinct Substrings,求不同的字串个数!
DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...
- SPOJ QTREE3 Query on a tree again! ——Link-Cut Tree
[题目分析] QTREE2,一看是倍增算法,太懒了,不写了.( ̄_, ̄ ) QTREE3,树链剖分可以做,发现链上的问题LCT也很好做. 要是子树问题貌似可以DFS序. 然后就成LCT模板题了. 考前 ...
- Linux之VMware虚拟机取消DHCP
1.点击编辑项 2.选择VMnet1 点击更改设置 3.选择VMnet1 去掉使用本地DHCP服务 点击应用 原文地址:https://blog.csdn.net/star_in_shy/arti ...
- vi 和vim 的区别以及用法
具体用法参考:http://blog.csdn.net/xuesnowce/article/details/53117352 它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所 ...
- POJ 2411 状压dp
F - Mondriaan's Dream Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I6 ...