图论Day39:孤岛题目

101. 孤岛的总面积

先用dfs遍历四条边界,把与之相连的置为0

再用计算孤岛面积的方法统计总面积

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){
    graph[x][y] = 0;
    for(int i = 0; i < 4; i++){
        int nextx = x + dir[i][0];
        int nexty = y + dir[i][1];
        if(nextx < 0 || nextx >= graph.size() || nexty < 0 || nexty >= graph[0].size()){
            continue;
        }
        if(graph[nextx][nexty] == 1){
            dfs(graph, nextx, nexty);
        }
    }
}
void dfs(vector<vector<int>>& graph, int x, int y, vector<vector<bool>>& visited, int &res){
    if(visited[x][y] == true){
        return;
    }
    res++;
    visited[x][y] = true;
    for(int i = 0; i < 4; i++){
        int nextx = x + dir[i][0];
        int nexty = y + dir[i][1];
        if(nextx < 0 || nextx >= graph.size() || nexty < 0 || nexty >= graph[0].size()){
            continue;
        }
        if(graph[nextx][nexty] == 1){
            dfs(graph, nextx, nexty, visited, res);
        }
    }
}
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];
        }
    }
    for(int i = 0; i < N; i++){
        if(graph[i][0] == 1){
            dfs(graph, i, 0);
        }
        if(graph[i][M - 1] == 1){
            dfs(graph, i, M - 1);
        }
    }
    for(int j = 0; j < M; j++){
        if(graph[0][j] == 1){
            dfs(graph, 0, j);
        }
        if(graph[N - 1][j] == 1){
            dfs(graph, N - 1, 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){
                dfs(graph, i, j, visited, res);
            }
        }
    }
    cout << res;
    return 0;
}

102. 沉没孤岛

先将四条边相连的地块都置为2,最后再全部遍历,将为1 为0的都置零,为2的置为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){
    graph[x][y] = 2;
    for(int i = 0; i < 4; i++){
        int nextx = x + dir[i][0];
        int nexty = y + dir[i][1];
        if(nextx < 0 || nextx >= graph.size() || nexty < 0 || nexty >= graph[0].size()){
            continue;
        }
        if(graph[nextx][nexty] == 1){
            dfs(graph, 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];
        }
    }
    for(int i = 0; i < N; i++){
        if(graph[i][0] == 1){
            dfs(graph, i, 0);
        }
        if(graph[i][M - 1] == 1){
            dfs(graph, i, M - 1);
        }
    }
    for(int j = 0; j < M; j++){
        if(graph[0][j] == 1){
            dfs(graph, 0, j);
        }
        if(graph[N - 1][j] == 1){
            dfs(graph, N - 1, j);
        }
    }
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M - 1; j++){
            if(graph[i][j] == 1){
                graph[i][j] = 0;
            }
            if(graph[i][j] == 2){
                graph[i][j] = 1;
            }
            cout << graph[i][j] << " ";
        }
        if(graph[i][M - 1] == 2){
            graph[i][M - 1] = 1;
        }
        cout << graph[i][M - 1] << endl;
    }
    return 0;
}

103. 高山流水

可以从边往中间搜,这样只用搜索四条边,不需要全部遍历

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, vector<vector<bool>>& Border, int x, int y){
    if(Border[x][y]){
        return;
    }
    Border[x][y] = true;
    for(int i = 0; i < 4; i++){
        int nextx = x + dir[i][0];
        int nexty = y + dir[i][1];
        if(nextx < 0 || nextx >= graph.size() || nexty < 0 || nexty >= graph[0].size()){
            continue;
        }
        if(graph[nextx][nexty] >= graph[x][y]){
            dfs(graph, Border, 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];
        }
    }

    vector<vector<bool>> firstBorder(N, vector<bool>(M, false));
    vector<vector<bool>> secondBorder(N, vector<bool>(M, false));

    for(int i = 0; i < N; i++){
        dfs(graph, firstBorder, i, 0);
        dfs(graph, secondBorder, i, M - 1);
    }
    for(int j = 0; j < M; j++){
        dfs(graph, firstBorder, 0, j);
        dfs(graph, secondBorder, N - 1, j);
    }
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            if(firstBorder[i][j] && secondBorder[i][j]){
                cout << i  << " " << j << endl;
            }
        }
    }
    return 0;
}

104. 建造最大岛屿

遍历二维网格,通过将一个海洋格子(0)改为陆地(1),找到改造后能形成的最大岛屿面积(岛屿为上下左右相邻的陆地)。

标记岛屿 & 统计面积

遍历网格,对每个未标记的陆地(1),用 DFS 遍历其连通区域,将该区域所有格子标记为唯一岛屿编号(从 2 开始);

记录每个岛屿编号对应的面积,同时保存原始最大岛屿面积(无改造时的最大值)。

计算改造海洋的最大收益

遍历每个海洋格子(0),统计其四个方向相邻的不同岛屿面积之和(用集合去重,避免重复加同一岛屿);

该和 + 1(改造的海洋格子本身)即为改造后岛屿面积,更新全局最大面积。

初始最大面积设为 1(应对全海洋场景:改造 1 个格子成陆地,面积为 1);

遍历海洋时先做边界检查,避免数组越界。

cpp 复制代码
#include <vector>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
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, int &areaSum, int islandId){
    graph[x][y] = islandId;
    areaSum++;
    for(int i = 0; i < 4; i++){
        int nextx = x + dir[i][0];
        int nexty = y + dir[i][1];

        if(nextx < 0 || nexty < 0 || nextx >= graph.size() || nexty >= graph[0].size()){
            continue;
        }
        if(graph[nextx][nexty] == 1){
            dfs(graph, nextx, nexty, areaSum, islandId);
        }
    }

}

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 islandId = 2;
    int res = 1;
    unordered_map<int, int> mp;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            if(graph[i][j] == 1){
                int sum = 0;
                dfs(graph, i, j, sum, islandId);
                mp[islandId] = sum;
                islandId++;
                res = max(res, sum);
            }
        }
    }

    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            if(graph[i][j] == 0){
                int curSum = 1;
                unordered_set<int> st;
                for(int k = 0; k < 4; k++){
                    int nextx = i + dir[k][0];
                    int nexty = j + dir[k][1];
                    if(nextx < 0 || nexty < 0 || nextx >= graph.size() || nexty >= graph[0].size()){
                        continue;
                    }
                    if(graph[nextx][nexty] == 0 || st.count(graph[nextx][nexty]) != 0) {
                        continue;
                    }

                    else {
                        curSum += mp[graph[nextx][nexty]];
                        res = max(res, curSum);
                        st.insert(graph[nextx][nexty]);
                    }
                }
            }
        }
    }
    cout << res;
    return 0;
}
相关推荐
LabVIEW开发5 分钟前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
严肃小白兔2 小时前
MATLAB 数值计算中的精度与误差问题:浮点运算、误差传播与数值稳定性
人工智能·算法·matlab
青少儿编程课堂2 小时前
蓝桥杯青少组图形化省考报名倒计时·全国青少年信息素养大赛·电子学会图形化考级:校园黑白棋人机对战解析
c++·python·算法·bfs·信息学竞赛
jktantan3 小时前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构
Dream it possible!16 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊17 小时前
算法之双指针系列(一)
算法·leetcode
十二17 小时前
【洛谷题解】P1083 [NOIP 2012 提高组] 借教室(二分答案 + 差分)
数据结构·c++·算法·蓝桥杯
小此方18 小时前
[算法实战论:动态规划]回文串问题的dp解法,回文串问题的母题,以及以此为基础的派生题
算法·动态规划
Pniubi18 小时前
力扣560.和为k的子数组
算法·leetcode·职场和发展
D e re k ZH19 小时前
C++ 从零入门(一):基础语法与程序结构
数据结构·c++·算法