迷宫问题 (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, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
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
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
解题思路:
bfs,用pre记录上一个位置,最后用回溯的方法输出路径。
代码:
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
int b[][];
int a[][];
int next[][]={{,},{-,},{,},{,-}};
struct node
{
int x,y;
int pre;
}que[];
int tx,ty; void print(int s)
{
if(que[s].pre!=-)
{
print(que[s].pre);
printf("(%d, %d)\n",que[s].x,que[s].y);
}
} void bfs(int x,int y)
{
int head=,tail=;
que[tail].x=x;
que[tail].y=y;
que[tail].pre=-;
tail++;
while(head<tail)
{
for(int i=;i<;i++)
{
tx=que[head].x+next[i][];
ty=que[head].y+next[i][];
if(tx<||tx>=||ty<||ty>=)continue;
if(b[tx][ty]==&&a[tx][ty]==)
{
b[tx][ty]=;
que[tail].x=tx;
que[tail].y=ty;
que[tail].pre=head;
tail++;
}
if(tx==&&ty==)
print(head);
}
head++;
}
} int main()
{
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
scanf("%d",&a[i][j]);
}
}
memset(b,,sizeof(b));
b[][]=;
printf("(0, 0)\n");
bfs(,);
printf("(4, 4)\n");
return ;
}
迷宫问题 (bfs广度优先搜索记录路径)的更多相关文章
- 算法竞赛——BFS广度优先搜索
BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...
- BFS广度优先搜索 poj1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...
- 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想
dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...
- 图的遍历BFS广度优先搜索
图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...
- 关于宽搜BFS广度优先搜索的那点事
以前一直知道深搜是一个递归栈,广搜是队列,FIFO先进先出LILO后进后出啥的.DFS是以深度作为第一关键词,即当碰到岔道口时总是先选择其中的一条岔路前进,而不管其他岔路,直到碰到死胡同时才返回岔道口 ...
- 步步为营(十六)搜索(二)BFS 广度优先搜索
上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...
- hdu 1664(数论+同余搜索+记录路径)
Different Digits Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Pots POJ - 3414 (搜索+记录路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22688 Accepted: 9626 Special J ...
- GraphMatrix::BFS广度优先搜索
查找某一结点的邻居: virtual int firstNbr(int i) { return nextNbr(i, n); } //首个邻接顶点 virtual int nextNbr(int i, ...
随机推荐
- 网络编程-Mysql-2、各种查询
1.先创建一个学生表 create table students ( id int auto_increment not null primary key, name varchar(20) not ...
- ubantu中执行docker免sudo方法
1.添加用户组,如果已存在则不用设置. sudo groupadd docker 2.将用户加入该 group (docker)内 sudo gpasswd -a ${USER} docker 3.重 ...
- Android源代码下载 “Gerrit下载源代码”
repo init -u ssh://jenkins@gerrit.y:29419/manifest -m k86A.xml 使用-m参数指定具体使用的是k86A.mxl文件 步骤1. curl ht ...
- 面试中遇到的原生js题总结
最近面试,遇到很多js相关的面试题,总结一下. 1.js 去重 1) indexOf Array.prototype.unique = function(){ var result = []; var ...
- Laravel使用redis保存SESSION
Laravel使用redis保存SESSION 首先确认服务器已经安装redis服务,php安装了redis扩展. 1.打开config/database.php.在redis配置项中增加sessio ...
- VS2013安装Boost
boost的编译和使用,经过搜集资料和总结,记录成文.感谢文后所列参考资料的作者. 1 下载 地址:http://sourceforge.net/projects/boost/files/boost/ ...
- php获取文章的第一张图片
今天做东西的时候遇到一个问题就是如何把文章提取出来作为文章列表呢? 因为用了Ueditor,所以提交的数据包含了html标签. 搜索了一会找到了一个方案,用php自带的函数去掉了html标签. $ar ...
- vue-cli 脚手架 Command Line Interface
mac sudo npm install -g nrm sudo npm config -g set unsafe-perm sudo npm install webpack@3.0.0 -g sud ...
- django 创建model(数据库表)失败
在models.py中创建数据库表 from django.db import models # Create your models here. class Book(models.Model) ...
- C. Vasya and Robot二分
1.题目描述 Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell ...