图论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;
}
相关推荐
张李浩4 小时前
Leetcode 054螺旋矩阵 采用方向数组解决
算法·leetcode·矩阵
big_rabbit05024 小时前
[算法][力扣101]对称二叉树
数据结构·算法·leetcode
美好的事情能不能发生在我身上4 小时前
Hot100中的:贪心专题
java·数据结构·算法
2301_821700535 小时前
C++编译期多态实现
开发语言·c++·算法
xixihaha13245 小时前
C++与FPGA协同设计
开发语言·c++·算法
小小怪7505 小时前
C++中的函数式编程
开发语言·c++·算法
xixixiLucky6 小时前
编程入门算法题---小明爬楼梯求爬n层台阶一共多少种方法
算法
剑锋所指,所向披靡!6 小时前
数据结构之线性表
数据结构·算法
m0_672703318 小时前
上机练习第49天
数据结构·算法
样例过了就是过了8 小时前
LeetCode热题100 N 皇后
数据结构·c++·算法·leetcode·dfs·深度优先遍历