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

#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. Linux使用期间命令积累

    1.调出终端 Ctrl+Alt+t 2.sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,如halt,reboot,su等等. sudo apt-g ...

  2. ssh proxy配置

    在.ssh目录下新建config文件,并添加以下内容: Host 10.100.101.123 ProxyCommand=nc -X 5 -x [proxyip:proxyport] %h %p

  3. hdu_3063_Play game(数论)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3063 题意:中文题,说的很清楚,不解释 题解:公式题,具体看代码 #include<stdio. ...

  4. tar.gz tar.bz2 解压

    从网络上下载到的源码包, 最常见的是 .tar.gz 包, 还有一部分是 .tar.bz2包   要解压很简单 :   .tar.gz     格式解压为          tar   -zxvf   ...

  5. Long-Polling, Websockets, SSE(Server-Sent Event), WebRTC 之间的区别

    在下面的示例中,客户端指的是浏览器,服务器指的是网站服务器主机. 为了更好的理解这些知识点,你应该简单了解典型的http网站是如何工作的. 普通的http: 客户端从服务器端请求网页 服务器作出相应的 ...

  6. WEBROOT根目录 <%=request.getContextPath()%>

    WEBROOT根目录 <%=request.getContextPath()%> == ${pageContext.request.contextPath}

  7. 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D

    http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...

  8. sql server 数据库导出表里所有数据成insert 语句

    有时候,我们想把数据库的某张表里的所有数据导入到另外一个数据库或另外一台计算机上的数据库,对于sql server有这样的一种方法 下面我以sql server 2008 R2,数据库是Northwi ...

  9. 直接用postman测试api ,服务器端没提供跨域也可以访问。

    1. 直接用postman测试api ,服务器端没提供跨域也可以访问. 但是,如果用本地的 sever 搭的server, 然后去访问api的话,浏览器会提示 跨域错误.

  10. java操作oracle的blob,clob数据

    一.区别和定义 LONG: 可变长的字符串数据,最长2G,LONG具有VARCHAR2列的特性,可以存储长文本一个表中最多一个LONG列 LONG RAW: 可变长二进制数据,最长2G CLOB:  ...