主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~

#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1005 using namespace std; int Map[MAX][MAX],n,v[][]={{,},{,-},{,},{-,}}; struct node
{
int x,y,step;
}ans[MAX][MAX]; void Ans(int x,int y)
{
node put[];
int k=,a,b;
while(x!= || y!=)//通过回溯的过程得到我走过的路径
{
a=x;
b=y;
put[k].x=ans[x][y].x;
put[k++].y=ans[x][y].y; x=ans[a][b].x;
y=ans[a][b].y;
} for(int i=k-;i>=;i--)
{
printf("(%d, %d)\n",put[i].x,put[i].y);
}
printf("(4, 4)\n");
} void BFS()
{
queue<node>Q;
node a,next;
a.x=;
a.y=;
a.step=;
Q.push(a);
Map[][]=; while(!Q.empty())
{
a=Q.front();
Q.pop(); if(a.x== && a.y==)
{
Ans(,);
return;
} for(int i=;i<;i++)
{
next.x=a.x+v[i][];
next.y=a.y+v[i][]; if(next.x>= && next.x< && next.y>= && next.y< && Map[next.x][next.y]==)
{
Map[next.x][next.y]=;
next.step=a.step+;
ans[next.x][next.y].x=a.x;//记录上一个点
ans[next.x][next.y].y=a.y;
Q.push(next);
}
}
}
} int main()
{
int i,j; for(i=;i<;i++)
for(j=;j<;j++)
scanf("%d",&Map[i][j]); BFS(); return ;
}

由于POJ上只有一组数据,你的代码就算A了也不一定正确,下面给出几组数据吧。

输入:
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0 0 0 0 0 0
0 1 1 1 0
0 1 0 0 0
0 1 1 1 0
0 0 0 1 0 0 0 0 0 0
0 1 1 1 0
0 1 0 0 0
0 0 1 0 1
1 0 0 0 0 0 0 0 0 0
0 1 1 1 0
1 0 0 0 0
1 0 1 0 1
1 0 0 0 0 输出:
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4) (0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 4)
(2, 4)
(3, 4)
(4, 4) (0, 0)
(1, 0)
(2, 0)
(3, 0)
(3, 1)
(4, 1)
(4, 2)
(4, 3)
(4, 4) (0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 4)
(2, 4)
(2, 3)
(3, 3)
(4, 3)
(4, 4)

POJ 3984 迷宫问题 记录路径的广搜的更多相关文章

  1. 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, ...

  2. POJ 3414 Pots 记录路径的广搜

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  4. POJ 3984 迷宫问题(简单bfs+路径打印)

    传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  6. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

  8. poj 3984 迷宫问题 bfs

    学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...

  9. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

随机推荐

  1. MyBatis学习-SQL 符号篇

    当我们需要通过 XML 格式处理 SQL 语句时,经常会用到 <,<=,>,>= 等符号,但是很容易引起 XML 格式的错误,这样会导致后台将 XML 字符串转换为 XML文档 ...

  2. undefine refrence to "*******"

    windows mingw gcc  编译出现莫名的错误  wsaaddresstostringa, 理解起来,应该是link的时候,出现的问题 (在console的日志栏也能看出来) 然后,在ECL ...

  3. WinDbg抓取程序报错dump文件的方法

    程序崩溃的两种主要现象: a. 程序在运行中的时候,突然弹出错误窗口,然后点错误窗口的确定时,程序直接关闭 例如: “应用程序错误” “C++错误之类的窗口” “程序无响应” “假死”等 此种崩溃特点 ...

  4. php file取重复

    function FetchRepeatMemberInArray($array) { // 获取去掉重复数据的数组 $unique_arr = array_unique ( $array ); // ...

  5. 一起来用css画画

    hello,大白来了... <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> ...

  6. 关于stringWithFormat: - 两段NSString如何合成一段

    http://blog.sina.com.cn/s/blog_6b1e4a0601019pib.html str = [NSString stringWithFormat:@"%@,%@&q ...

  7. 改变vim配色:安装colorscheme【转】

    主要有两种方式安装colorscheme: 自行下载colorscheme安装,下载的文件扩展名通常为.vim. 通过安装相关vim的插件获取. 自行下载colorscheme安装 以mac为例,在系 ...

  8. Inno Setup入门(六)——在程序目录下创建文件

    创建文件夹可以使用[dirs]段实现,代码如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST DefaultDirName="E:\TES ...

  9. kill -0

    http://unix.stackexchange.com/questions/169898/what-does-kill-0-do 检查有没有权限杀他

  10. 使用固件库操作STM32F4时的必要配置(转)

    源:使用固件库操作STM32F4时的必要配置 使用STM32F4的固件库时,默认的晶振为25Mhz晶振,因此需要做一定的修改.之前因为一直没有注意这个问题,我捣腾了许久,发现工作时钟总是不对,查阅了一 ...