图论Day38:孤岛基础

99. 计数孤岛

在遍历图的过程中,每次遇到1就使用深搜/广搜将所有相连的地块都变成0,继续遍历,遇到新的1就是新岛屿

深搜

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;

int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(vector<vector<int>>& graph, int x, int y){
    if(graph[x][y] == 1){
        graph[x][y] = 0;
        for(int i = 0; i < 4; i++){
            if (x + dir[i][0] < 0 || x + dir[i][0] >= graph.size() || y + dir[i][1] < 0 || y + dir[i][1] >= graph[0].size()) 
                continue;
            dfs(graph, x + dir[i][0], y + dir[i][1]);
        }
    }
}
int main(){
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N, vector<int>(M, 0));
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++){
            cin >> graph[i][j];
        }
    }
    int res = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++){
            if(graph[i][j] == 1){
                dfs(graph, i, j);
                res++;
            }
        }
    }
    cout << res;
    return 0;
}

广搜

cpp 复制代码
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void bfs(vector<vector<int>>& graph, int x, int y){
    if(graph[x][y] == 0){
        return;
    }
    queue<pair<int, int>> que;
    que.push({x, y});
    graph[x][y] = 0;
    while(!que.empty()){
        int curx = que.front().first;
        int cury = que.front().second;
        que.pop();

        for(int i = 0; i < 4; i++){
            int nextx = curx + dir[i][0];
            int nexty = cury + dir[i][1];
            if(nextx < 0 || nextx >= graph.size() || nexty < 0 || nexty >= graph[0].size()){
                continue;
            }
            if(graph[nextx][nexty] == 1){
                graph[nextx][nexty] = 0;
                que.push({nextx, nexty});
            }
        }
    }
}
int main(){
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N, vector<int>(M, 0));
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++){
            cin >> graph[i][j];
        }
    }
    int res = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++){
            if(graph[i][j] == 1){
                
                bfs(graph, i, j);
                res++;
            }
        }
    }
    cout << res;
    return 0;
}

100. 最大岛屿的面积

访问一块地块统计就加1,取最大的岛屿

cpp 复制代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(vector<vector<int>>& graph, vector<vector<bool>>&visited, int x, int y, int& count){
    visited[x][y] = true;
    count++;
    for(int i = 0; i < 4; i++){
        if(x + dir[i][0] < 0 || x + dir[i][0] >= graph.size() || y + dir[i][1] < 0 || y + dir[i][1] >= graph[0].size()){
            continue;
        }
        if(graph[x + dir[i][0]][y + dir[i][1]] == 1 && visited[x + dir[i][0]][y + dir[i][1]] == false){
            dfs(graph, visited, x + dir[i][0], y + dir[i][1], count);
        }
    }
}
int main(){
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N, vector<int>(M, 0));
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            cin >> graph[i][j];
        }
    }
    int res = 0;
    vector<vector<bool>>visited(N, vector<bool>(M, false));
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            if(graph[i][j] == 1){
                int count = 0;
                dfs(graph, visited, i, j, count);
                res = max(res, count);
            }
        }
    }
    cout << res;
    return 0;
}
相关推荐
2301_822703203 分钟前
开源鸿蒙跨平台Flutter开发:幼儿疫苗全生命周期追踪系统:基于 Flutter 的免疫接种档案与状态机设计
算法·flutter·华为·开源·harmonyos·鸿蒙
贵慜_Derek5 分钟前
Managed Agents 里,Harness 到底升级了什么?
人工智能·算法·架构
2301_8227032031 分钟前
鸿蒙flutter三方库实战——教育与学习平台:Flutter Markdown
学习·算法·flutter·华为·harmonyos·鸿蒙
Jia ming42 分钟前
C语言实现日期天数计算
c语言·开发语言·算法
无限进步_1 小时前
【C++&string】大数相乘算法详解:从字符串加法到乘法实现
java·开发语言·c++·git·算法·github·visual studio
苏纪云1 小时前
蓝桥杯考前突击
c++·算法·蓝桥杯
W23035765732 小时前
经典算法详解:最长公共子序列 (LCS) —— 从暴力递归到动态规划完整实现
算法·动态规划·最长子序列
pzx_0012 小时前
【优化器】 随机梯度下降 SGD 详解
人工智能·python·算法
小肝一下2 小时前
每日两道力扣,day8
c++·算法·leetcode·哈希算法·hot100
无限进步_2 小时前
【C++】验证回文字符串:高效算法详解与优化
java·开发语言·c++·git·算法·github·visual studio