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

#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. [bzoj3196][Tyvj 1730][二逼平衡树] (线段树套treap)

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在 ...

  2. ShellExecute, WinExec, CreateProcess区别

    ShellExecute ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制. 有几个API函数都可以实现这些功能 ...

  3. Mac下截图快捷键

    Cmd+Shift+3:全屏截图:截取整个屏幕,保存截图至桌面文件夹.Cmd+Shift+4:区域截图:鼠标光标变成带坐标的小十字,通过拖拽截取特定区域,保存截图至桌面文件夹.Cmd+Shift+4 ...

  4. 关于Spring Security 3获取用户信息的问题

    标签: spring security 3标签获取用户信息 2013-01-05 10:40 5342人阅读 评论(0) 收藏 举报  分类: Spring(25) java(70) 前端(7)    ...

  5. 官方windows10升级工具

    https://www.microsoft.com/zh-cn/software-download/windows10

  6. wpa_supplicant wpa_cli 的使用说明

    wpa_supplicant -d -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf -iwlan0 -B   会在/data/misc/wifi/下产生 ...

  7. Photoshop 字体

    字体类型: 宋体系列:北宋年间 超粗宋.粗宋,大标宋.小标宋/中宋.宋体 黑体系列:建国初期 (当时写标语,识别度高) 超粗黑 大黑/黑体 中等线/细黑 --广告.画册.宣传册 非正规场合 文字工具( ...

  8. TextUtils判断

    System.out.println(TextUtils.isEmpty(null)); System.out.println(TextUtils.isEmpty(""));

  9. HttpWebRequest 抓取页面异常处理办法

    抓取页面异常处理办法 public static string GetHtmlTest(string URI) { string fullhtml = null; while (true) { try ...

  10. Oracle自增长序列

    create table user_info ( id number(6) primary key, username varchar2(30) not null, password varchar2 ...