DFS连通块问题

DFS连通块问题

只要是位置的上下左右都有 '1' 的就是同一块连通块

一般题目类型有:

  1. 连通块中数量最多的连通块
  2. 联通块的数量
c++ 复制代码
110010000011111110101001001001101010111011011011101001111110
010000000001010001101100000010010110001111100010101100011110 
001011101000100011111111111010000010010101010111001000010100 
101100001101011101101011011001000110111111010000000110110000 
010101100100010000111000100111100110001110111101010011001011 
010011011010011110111101111001001001010111110001101000100011 
101001011000110100001101011000000110110110100100110111101011 
101111000000101000111001100010110000100110001001000101011001 
001110111010001011110000001111100001010101001110011010101110 
001010101000110001011111001010111111100110000011011111101010 
011111100011001110100101001011110011000101011000100111001011 
011010001101011110011011111010111110010100101000110111010110 
001110000111100100101110001011101010001100010111110111011011 
111100001000001100010110101100111001001111100100110000001101 
001110010000000111011110000011000010101000111000000110101101 
100100011101011111001101001010011111110010111101000010000111 
110010100110101100001101111101010011000110101100000110001010 
110101101100001110000100010001001010100010110100100001000011 
100100000100001101010101001101000101101000000101111110001010 
101101011010101000111110110000110100000010011111111100110010 
101111000100000100011000010001011111001010010001010110001010 
001010001110101010000100010011101001010101101101010111100101 
001111110000101100010111111100000100101010000001011101100001 
101011110010000010010110000100001010011111100011011000110010 
011110010100011101100101111101000001011100001011010001110011 
000101000101000010010010110111000010101111001101100110011100 
100011100110011111000110011001111100001110110111001001000111 
111011000110001000110111011001011110010010010110101000011111 
011110011110110110011011001011010000100100101010110000010011 
010011110011100101010101111010001001001111101111101110011101

最大连通 - 蓝桥云课 (lanqiao.cn)

c++ 复制代码
#include <bits/stdc++.h>
using namespace std;
int n = 30, m = 60, ans = 0, res = 0;
const int cz[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
vector<vector<char>> sp(100, vector<char>(100));
vector<vector<bool>> vis(100, vector<bool>(100, true));

void dfs(int x, int y)
{
    vis[x][y] = false;
    ans++;
    for (int i = 0; i < 4; ++i)
    {
        int tx = x + cz[i][0];
        int ty = y + cz[i][1];
        if (tx < 1 || ty < 1 || tx > n || ty > m)
            continue;
        if (vis[tx][ty] && sp[tx][ty] == '1')
            dfs(tx, ty);
    }
}
void solve()
{
    for (int i = 1; i <= 30; ++i)
        for (int j = 1; j <= 60; ++j)
            cin >> sp[i][j];

    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
        {
            if (vis[i][j] && sp[i][j] == '1')
            {
                ans = 0;
                dfs(i, j);
                res = max(res, ans);
            }
        }
    cout << res << endl;
}
int main()
{
    solve();
    return 0;
}
相关推荐
njsgcs1 分钟前
已知链中心距用节数算中间电机摆放位置 常量弧长 三链轮弦长模型算法
算法
aaPIXa62228 分钟前
C++ TensorRT Edge-LLM 边缘推理框架:从原理到实战
开发语言·c++·edge
2601_962440841 小时前
计算机毕业设计之jsp旅游网站管理系统
java·开发语言·算法·课程设计·旅游·推荐算法
池塘的蜗牛1 小时前
AFDM波形基于DFT的通感一体化
算法
147API2 小时前
Claude global workspace 研究给智能体测试提了一个醒
人工智能·算法·机器学习
葫三生2 小时前
《论三生原理》与模糊数学的关联、异同、互补关系?
人工智能·科技·算法·机器学习·开源
我命由我123452 小时前
方差(实例实操、与标准差的区别)
java·数据结构·算法·数据分析·java-ee·intellij-idea·idea
闲研随记2 小时前
【文献阅读 ICML 2026】RL算法:Critique-GRPO
论文阅读·算法·llm·强化学习·rl·icml·grpo
nianniannnn2 小时前
c++复习自存--标准模板库STL
开发语言·c++·windows
民乐团扒谱机2 小时前
【微实验】从迷雾中看清世界:卡尔曼滤波的数学诗意与MATLAB实践
人工智能·算法·matlab·卡尔曼滤波·kalman