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

#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. android 联网

    <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="htt ...

  2. HDU 3416 Marriage Match IV

    最短路+最大流 #include<cstdio> #include<cstring> #include<string> #include<cmath> ...

  3. Python第三方库安装技巧

    pytho下有三种安装第三方库方法: 1.通过easy_install安装 2.通过Pip安装 前面两种,由于受国内部门网站原因,如果安装失败,可采用接下来的第三种方法 3.在指定网站下载安装 第三方 ...

  4. servlet第2讲(上集)----创建servlet实例(实现servlet接口)

  5. flex、As 3.0 小知识

    <s:CheckBox  id="checkBox" x="10" y="5" label="{new ObjectProx ...

  6. webstrom管理git

    先写一段 webstrom文件名变色:1.绿色的文件添加 2.蓝色的原有文件修改 如果出现““No such file or directory”或类似的语句,说明缺少ssh的key.那么我们就得创建 ...

  7. C# 系统应用之清除Cookies、IE临时文件、历史记录 转载

    http://blog.csdn.net/Eastmount/article/details/18821221 本文主要是项目"个人电脑使用记录清除软件"系类文章中关于清除浏览器C ...

  8. 如何清除jboss缓存

    要清除Jboss下的缓存,只要清除以下文件的所有文件就可以了:1.D:\JavaServer\jboss-4.2.2.GA\server\default\tmp2.D:\JavaServer\jbos ...

  9. MySQL函数大全【转载】

    转载自 http://www.jb51.net/article/40179.htm 一.数学函数ABS(x)   返回x的绝对值BIN(x)   返回x的二进制(OCT返回八进制,HEX返回十六进制) ...

  10. Windows GTK+ 环境搭建(详解)

    来源:http://blog.sina.com.cn/s/blog_a6fb6cc901017ygy.html Windows GTK+ 环境搭建 最近要做界面的一些东西,但是对微软提供的类库MFC不 ...