广度优先搜索(Breadth First Search, BFS)
广度优先搜索(Breadth First Search, BFS)
BFS算法实现的一般思路为:
// BFS
void BFS(int s){
queue<int> q; // 定义一个队列
q.push(s); // 队首元素入队 while (!q.empty()){
// 取出队首元素top
// 访问队首元素
// 将队首元素出队
// 将top的下一层结点中未曾入队的结点全部入队,并设置为已入队
}
}
常见题型一:

代码实现:
#include <stdio.h>
#include <queue>
using namespace std; const int maxn = ; // 位置结构体
struct node{
int x, y; // 位置(x, y)
}Node; int n, m; // 矩阵大小为 n * m
int matrix[maxn][maxn]; // 01 矩阵
bool inq[maxn][maxn] = { false }; // 记录位置 (x, y) 是否已入过队
int X[] = { , , , - }; // 增量数组
int Y[] = { , -, , }; // 判断坐标(x, y)是否需要访问
bool judge(int x, int y){
// 越界访问false
if (x >= m || x < || y >= n || y < ){
return false;
}
// 当前位置为0或者已经入过队也返回false
if (matrix[x][y] == || inq[x][y] == true){
return false;
}
// 否则返回 true
return true;
} // BFS函数访问位置(x, y)所在的块,将该块的所有'1'的inq都设置为 true
void BFS(int x, int y){
// 定义一个队列
queue<node> Q;
// 队首元素入队
Node.x = x, Node.y = y;
Q.push(Node); // 队列不为空则一直循环
while (!Q.empty()){
// 取出队首元素
node top = Q.front();
// 访问队首元素
// 弹出队首元素
Q.pop();
// 将这个元素所相连的坐标设置为已入队
for (int i = ; i < ; i++){
int newX = top.x + X[i];
int newY = top.y + Y[i];
if (judge(newX, newY)){
Node.x = newX, Node.y = newY;
// 将所有相连坐标入队
Q.push(Node);
inq[newX][newY] = true; // 设置位置[newX, newY]为已入过队
}
}
}
} int main()
{
// 读取输入
scanf("%d %d", &m, &n);
for (int i = ; i < m; i++){
for (int j = ; j < n; j++){
scanf("%d", &matrix[i][j]); // 读入 01 矩阵
}
int ans = ; // 存放块数
// 遍历矩阵
for (int x = ; x < m; x++){
for (int y = ; j < n; y++){
// 入过位置为1 且没有入过队则计数器加一
if (matrix[x][y] == && inq[x][y] == false){
ans++;
BFS(x, y);
}
}
}
} printf("%d\n", ans); return ;
}
常见题型二:

代码实现:
#include <stdio.h>
#include <queue>
using namespace std; const int maxn = ;
struct node{
int x, y;
int step; // step 为从起点到终点位置最少的步数(即层数)
}S, T, temp; int m, n; // n 为行, m位列
char maze[maxn][maxn]; // 迷宫信息
bool inq[maxn][maxn] = { false };
int X[] = { , , , - };
int Y[] = { , -, , }; // 检测位置(x, y)是否有效
bool test(int x, int y){
if (x >= m || x < || y >= n || y < )
return false;
if (maze[x][y] == '*' || inq[x][y] == true)
return false;
return true;
} int BFS(){
queue<node> q;
q.push(S); while (!q.empty()){
node top = q.front();
if (top.x == T.x && top.y == T.y)
return top.step;
q.pop();
for (int i = ; i < ; i++){
int newX = top.x + X[i];
int newY = top.y + Y[i];
if (test(newX, newY)){
// 创建一个新结点
node temp;
temp.x = newX, temp.y = newY;
temp.step = top.step + ;
q.push(temp);
inq[newX][newY] = true;
}
}
}
return -;
} int main()
{
scanf("%d %d", &m, &n);
for (int i = ; i < m; i++){
for (int j = ; j < n; j++){
maze[i][j] = getchar();
}
maze[i][n] = '\0';
}
scanf("%d %d %d %d", &S.x, &S.y, &T.x, &T.y);
S.step = ;
printf("%d\n", BFS()); return ;
}
广度优先搜索(Breadth First Search, BFS)的更多相关文章
- 数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索( ...
- javascript实现的图数据结构的广度优先 搜索(Breadth-First Search,BFS)和深度优先搜索(Depth-First Search,DFS)
最后一例,搞得快.三天之内走了一次.. 下一步,面象对像的javascript编程. function Dictionary(){ var items = {}; this.has = functio ...
- 算法与数据结构基础 - 广度优先搜索(BFS)
BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...
- [SOJ] 图的广度优先搜索
Time Limit: 1sec Memory Limit:256MB Description 读入图的邻接矩阵以及一个顶点的编号(图中顶点的编号为从1开始的连续正整数.顶点在邻接矩阵的行和列上 ...
- (转)广度优先搜索BFS和深度优先搜索DFS
1. 广度优先搜索介绍 广度优先搜索算法(Breadth First Search),又称为"宽度优先搜索"或"横向优先搜索",简称BFS. 它的思想是:从图中 ...
- 关于宽搜BFS广度优先搜索的那点事
以前一直知道深搜是一个递归栈,广搜是队列,FIFO先进先出LILO后进后出啥的.DFS是以深度作为第一关键词,即当碰到岔道口时总是先选择其中的一条岔路前进,而不管其他岔路,直到碰到死胡同时才返回岔道口 ...
- 常用算法2 - 广度优先搜索 & 深度优先搜索 (python实现)
1. 图 定义:图(Graph)是由顶点的有穷非空集合和顶点之间边的集合组成,通常表示为:G(V,E),其中,G表示一个图,V是图G中顶点的集合,E是图G中边的集合. 简单点的说:图由节点和边组成.一 ...
- 【js数据结构】图的深度优先搜索与广度优先搜索
图类的构建 function Graph(v) {this.vertices = v;this.edges = 0;this.adj = []; for (var i = 0; i < this ...
- [MIT6.006] 13. Breadth-First Search (BFS) 广度优先搜索
一.图 在正式进入广度优先搜索的学习前,先了解下图: 图分为有向图和无向图,由点vertices和边edges构成.图有很多应用,例如:网页爬取,社交网络,网络传播,垃圾回收,模型检查,数学推断检查和 ...
随机推荐
- C# monitor keyboard and print pressed key
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnos ...
- 【IDE】IDEA - 使用问题记录
1.显示方法API:类似eclipse把鼠标放到方法上有api信息. setting/keymap中搜索 quick docs:(有说默认快捷键是ctrl+q,但我的不是待查找快捷键) 2.forea ...
- 星星评分-依赖jquery
https://pan.baidu.com/s/1UWJFh-QJOjSB_yqA8VgHIQ
- JPA 常用注解
@Entity(name=”EntityName”):必须,name为可选,对应数据库中一的个表 @Table(name=””,catalog=””,schema=””):可选 通常和@Entity配 ...
- 转载 selenium_对浏览器操作、鼠标操作等总结
https://www.jianshu.com/p/7a4414082ce2 查看环境conda info --env 激活环境conda activate machine 路径改成H:cd H:\p ...
- ls, chgrp, chown, chmod
ls命令 [root@client ~]# ls -la 总用量 dr-xr-x---. root root 2月 : . dr-xr-xr-x. root root 2月 : .. -rwxrwxr ...
- Mybatis Dao接口与Xml文件不匹配的问题:Invalid bound statement (not found)
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --> <resources> <resource> <directory&g ...
- ng-组件
几乎所有前端框架都在玩"组件化",而且最近都不约而同地选择了"标签化"这种思路,Angular 也不例外. 对新版本的 Angular 来说,一切都是围绕着&q ...
- ng-起步
目前,无论你使用什么前端框架,都必然要使用到各种 NodeJS 工具,Angular 也不例外.与其它框架不同,Angular 从一开始就走的"全家桶"式的设计思路,因此 @ang ...
- Redis 数据类型及操作
前言 作为Key-value型数据库,Redis也提供了键(Key)和键值(Value)的映射关系.但是,除了常规的数值或字符串,Redis的键值还可以是以下形式之一: Lists (可重复列表) S ...