求连通块数量模板 (BFS)

连通块模板(BFS)

大致思路就是选中一个点然后向四周拓展并标记,如果不能拓展了就为一个块,最后输出个数即可

复制代码
#include<iostream>
#include<queue> // 利用队列存储数据
using namespace std;
int n, m, cnt ; // 全局变量默认为0
int arr[105][105] ;
int book[105][105] ;
int dx[] = {-1, 0, 1, 0} ; // 利用两个一维数组来表示四个方向
int dy[] = {0, 1, 0, -1} ;
struct node 
{
	int x ;
	int y ;
};
queue<node> qu ;  // 建立一个存放结构体的队列
void bfs(int x1, int y1) // 传入起点坐标
{
	node a = {x1, y1} ;
	qu.push(a) ; // 把起点存入队首
	while (!qu.empty()) //队列非空是循环条件
	{
		node b = qu.front() ; // 取出队首
		qu.pop() ; // 去掉队首
		for (int i = 0 ; i < 4 ; i++) { // 利用循环遍历四个方向
			int nx = b.x + dx[i] ;
			int ny = b.y + dy[i] ;
			if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) continue ; // 判断边界
			if (arr[nx][ny] == 0 && book[nx][ny] == 0) { 
				book[nx][ny] = 1 ; // 把可拓展的地方标记起来
				node c = {nx, ny} ;
				qu.push(c) ;
			} // 如果四周没有可以拓展的地方,就会不断弹出队首,直到队列为空跳出循环
		}
	}	
}
int main(void)
{
	cin >> n >> m ;
	for(int i = 0 ; i < n ; i++) { // 读入二维数组
 		for (int j = 0 ; j < m ; j++) {
 			cin >> arr[i][j] ;
 		}
	}
	for (int i = 0 ; i < n ; i++) {
		for (int j = 0 ; j < m ; j++) {
			if (arr[i][j] == 0 && book[i][j] == 0) { // 寻找到可拓展却没被标记的作为起点
				cnt++ ;
				bfs(i, j) ;
			}
		} // 如果有多个样例记得将数组进行重置 
	} // 代码为: memset(arr, 0, sizeof(arr)) 在头文件<cstring>中,把数组重置为全为0
	cout << cnt << endl ;
	return 0 ;
}
相关推荐
RTC老炮4 分钟前
webrtc弱网-BitrateEstimator类源码分析与算法原理
网络·人工智能·算法·机器学习·webrtc
郝学胜-神的一滴5 分钟前
Linux下的阻塞与非阻塞模式详解
linux·服务器·开发语言·c++·程序人生·软件工程
程序员烧烤7 分钟前
【leetcode刷题007】leetcode116、117
算法·leetcode
一只小风华~19 分钟前
学习笔记:Vue Router 中的链接匹配机制与样式控制
前端·javascript·vue.js·笔记·学习·ecmascript
ghie90901 小时前
基于libsvm的支持向量机在MATLAB中的实现
算法·支持向量机·matlab
月临水2 小时前
Redis 学习笔记(二)
redis·笔记·学习
Nan_Shu_6142 小时前
学习SpringBoot
java·spring boot·后端·学习·spring
●VON2 小时前
重生之我在大学自学鸿蒙开发第二天-《MVVM模式》
学习·华为·harmonyos
Samsong2 小时前
《C++ Primer Plus》读书笔记 第二章 开始学习C++
c++·后端
你真的可爱呀3 小时前
uniapp学习【vue3在uniapp中语法,使用element,使用uView UI】
学习·uni-app