【Day44 LeetCode】图论问题 Ⅱ

一、图论问题 Ⅱ

1、岛屿的最大面积

这题和上一篇博客求岛屿数量如出一辙,都是要找出所有岛屿,深度优先搜索代码如下:

CPP 复制代码
# include<iostream>
# include<vector>

using namespace std;

int dfs(vector<vector<int>> &graph, int i, int j){
    if(i<0 || i>=graph.size() || j<0 || j>=graph[0].size() || graph[i][j]!=1)
        return 0;
    graph[i][j] = 2;
    return 1 + dfs(graph, i+1, j)+ dfs(graph, i-1, j)+ dfs(graph, i, j+1)+ dfs(graph, i, j-1);
}

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(m));
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            cin >> graph[i][j];
    
    int ans = 0;
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            if(graph[i][j]==1)
                ans = max(ans, dfs(graph, i, j));
    cout << ans << endl;
    return 0;
}

广度优先搜索代码如下:

CPP 复制代码
# include<iostream>
# include<vector>
#include<queue>

using namespace std;

vector<vector<int>> dirs({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
int bfs(vector<vector<int>> &graph, int ii, int jj){
    queue<pair<int, int>> q;
    q.push({ii, jj});
    graph[ii][jj] = 2;
    int res = 0;
    while(!q.empty()){
        auto cur = q.front(); q.pop();
        ++res;
        for(auto xy : dirs){
            int i = cur.first + xy[0], j = cur.second + xy[1];
            if(i<0 || i>=graph.size() || j<0 || j>=graph[0].size() || graph[i][j]!=1)
                continue;
            graph[i][j] = 2;
            q.push({i, j});
        }
    }
    return res;
}

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(m));
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            cin >> graph[i][j];
    
    int ans = 0;
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            if(graph[i][j]==1)
                ans = max(ans, bfs(graph, i, j));
    cout << ans << endl;
    return 0;
}

2、孤岛总面积

本质上还是要搜索所有岛屿,同时还得统计岛屿面积,将是孤岛的面积累加。这就涉及到不是孤岛的判断,遇到边界就不是孤岛,这个不要加入结果,我们只需要让函数的统计结果减去一个很大的数 ,从而保证不是孤岛的返回值是负数就好,最后结果只累加正数。这个能在之前的代码下做出最小的改动。

深度优先搜索代码如下:

CPP 复制代码
# include<iostream>
# include<vector>

using namespace std;

int dfs(vector<vector<int>> &graph, int i, int j){
    if(i<0 || i>=graph.size() || j<0 || j>=graph[0].size() || graph[i][j]!=1)
        return 0;
    graph[i][j] = 2;
    int res = 1;
    if(i==0 || j==0 || i==graph.size()-1 || j==graph[0].size()-1)
        res -= 10000;
    res += dfs(graph, i+1, j)+ dfs(graph, i-1, j)+ dfs(graph, i, j+1)+ dfs(graph, i, j-1);
    return res;
}


int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(m));
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            cin >> graph[i][j];
    int ans = 0;
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            if(graph[i][j]==1)
                ans += max(0, dfs(graph, i, j));
    cout << ans << endl;
    
    return 0;
}

广度优先搜索代码如下:

CPP 复制代码
# include<iostream>
# include<vector>
#include<queue>

using namespace std;

vector<vector<int>> dirs({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
int bfs(vector<vector<int>> &graph, int ii, int jj){
    queue<pair<int, int>> q;
    q.push({ii, jj});
    graph[ii][jj] = 2;
    int res = 0;
    while(!q.empty()){
        auto cur = q.front(); q.pop();
        ++res;
        if(cur.first==0 || cur.second==0 || cur.first==graph.size()-1 || cur.second==graph[0].size()-1)
            res -= 10000;
        for(auto xy : dirs){
            int i = cur.first + xy[0], j = cur.second + xy[1];
            if(i<0 || i>=graph.size() || j<0 || j>=graph[0].size() || graph[i][j]!=1)
                continue;
            graph[i][j] = 2;
            q.push({i, j});
        }
    }
    return res;
}

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(m));
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            cin >> graph[i][j];
    
    int ans = 0;
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            if(graph[i][j]==1)
                ans += max(0, bfs(graph, i, j));
    cout << ans << endl;
    return 0;
}
相关推荐
yuniko-n2 分钟前
【力扣 SQL 50】子查询篇
数据库·sql·leetcode
Andyshengwx3 分钟前
图论 最小生成树 MST问题
c++·算法·图论
賬號封禁中miu4 分钟前
图论之最小生成树
java·数据结构·算法·图论
脑海科技实验室4 分钟前
Ageing Res Rev:绘制阿尔茨海默病分期进展图:一项利用静息态fMRI和图论的综合性横断面及纵向研究
图论·fmri·阿尔茨海默病
闻缺陷则喜何志丹5 分钟前
【图论 拓扑排序 贪心 临项交换】P5603 小 C 与桌游 题解|普及+
c++·算法·图论·贪心·拓扑排序·洛谷·临项交换
闻缺陷则喜何志丹6 分钟前
【图论 BFS染色 并集查找 】P3663 [USACO17FEB] Why Did the Cow Cross the Road III S|普及+
c++·算法·图论·染色法·宽度优先·并集查找
月明长歌6 分钟前
Java数据结构:PriorityQueue堆与优先级队列:从概念到手写大根堆
java·数据结构·python·leetcode·
青山如墨雨如画7 分钟前
【北邮-研-图论】网络最大流的标号算法V1.0
网络·算法·图论·北邮
chao1898447 分钟前
基于MATLAB实现NSGA-II算法
开发语言·算法·matlab
mmz12079 分钟前
差分数组(c++)
c++·算法