求连通块(BFS、DFS例题)

题目:

给出一个mxn的矩阵,矩阵中的元素为0或1。称位置(x, y)与其上下左右四个位置(x,y+1)、 (x,y-1) (x+1,y) (x- 1, y)是相邻的。如果矩阵中有若干个1是相邻的(不必两两相邻),那么称这些1构成了一个“块”。求给定的矩阵中“块”的个数。
在这里插入图片描述

前言:记录一下思路,方便以后回来复习一下

思路与要点:

1、设置方向增量的数组:

int X[] = {
    
     0,0,1,-1 };		//方向增量数组
int Y[] = {
    
     1,-1,0,0 };

2、设置一个记录点坐标的结构体(pair也可啦)

struct nod
{
    
    
	int x, y;
}node;

3、记录所有点是否已经访问过,访问一个点就将他入队,用临时变量储存他,将他pop出队,for来广度搜索他四个方向的元素,之后分别进行进队出队最后用bool数组存储某个元素是否入队过,初始全为FALSE

bool inq[6][7] = {
    
     false };				//判断元素是否入队

void bfs(int x, int y)	//BFS算法
{
    
    
	queue<nod>q;		//nod型队列
	node.x = x, node.y = y;	//实参传递
	q.push(node);
	inq[x][y] = true;
	while (!q.empty())
	{
    
    
		nod top = q.front();		//取出栈顶
		q.pop();					//栈顶出栈
		for (int i = 0; i < 4; 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;
			}
		}
	}
}

4、每次访问时不必都进入BFS,写一个judge函数来判断他是否需要访问,如果该点已经入过队,或者超出边界,就跳过:

int main()
{
    
    
	int ans = 0;		//存放块数的答案
	for (int i = 0; i < 6; i++)
		for (int j = 0; j < 7; j++)
		{
    
    
			if (a[i][j] == 1 && inq[i][j] == false)//如果没被入队过并且点权等于1,那么就BFS,否则就下一个a[i][j]
			{
    
    
				ans++;
				bfs(i, j);
			}
		}
}

或是在BFS中访问判断:

bool judge(int x, int y)
{
    
    
	if (x >= 6 || x < 0 || y >= 7 || y < 0) return false;
	if (inq[x][y] == true || a[x][y] == 0) return false;
	//都不符合情况, 返回真
	return true;
}

BFS完整实现代码:

#include<algorithm>
#include<stdio.h>
#include<string.h>
#include <iostream>
#include<string>
#include<queue>
using namespace std;

struct nod
{
    
    
	int x, y;
}node;

int X[] = {
    
     0,0,1,-1 };		//方向增量数组
int Y[] = {
    
     1,-1,0,0 };

int a[6][7] = {
    
    			//01地图矩阵
			{
    
    0,1,1,1,0,0,1},
			{
    
    0,0,1,0,0,0,0},
			{
    
    0,0,0,0,1,0,0},
			{
    
    0,0,0,1,1,1,0},
			{
    
    1,1,1,0,1,0,0},
			{
    
    1,1,1,1,0,0,0}
};

bool inq[6][7] = {
    
     false };				//判断元素是否入队

bool judge(int x, int y)
{
    
    
	if (x >= 6 || x < 0 || y >= 7 || y < 0) return false;
	if (inq[x][y] == true || a[x][y] == 0) return false;
	//都不符合情况, 返回真
	return true;
}

void bfs(int x, int y)
{
    
    
	queue<nod>q;		//nod型队列
	node.x = x, node.y = y;	//实参传递
	q.push(node);
	inq[x][y] = true;
	while (!q.empty())
	{
    
    
		nod top = q.front();		//取出栈顶
		q.pop();					//栈顶出栈
		for (int i = 0; i < 4; 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;
			}
		}
	}
}

int main()
{
    
    
	int ans = 0;		//存放块数
	for (int i = 0; i < 6; i++)
		for (int j = 0; j < 7; j++)
		{
    
    
			if (a[i][j] == 1 && inq[i][j] == false)
			{
    
    
				ans++;
				bfs(i, j);
			}
		}
	for (int i = 0; i < 6; i++) {
    
    
		for (int j = 0; j < 7; j++)
			cout << a[i][j] << " ";
		cout << endl<<endl;
	}
	cout << "一共"<<ans<<"个联通分块!";
	return 0;
}

DFS函数实现:(dfs用的栈)

void dfs(int x, int y)
{
    
    
	stack<nod>w;		//队列	
	node.x = x, node.y = y;
	w.push(node);		//进栈
	inq[x][y] = true;	//记录进栈
	for (int i = 0; i < 4; i++)
	{
    
    
		if (judge(x + X[i], y + Y[i]))//符合条件就DFS
		{
    
    
			dfs(x + X[i], y + Y[i]);
		}
	}
	w.pop();		//如果四个方向访问后退回,代表没有路走了,出栈
}

测试:

后语:35Days

猜你喜欢

转载自blog.csdn.net/Kyrie_irving_kun/article/details/114790371