【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;
}
相关推荐
wen__xvn36 分钟前
每日一题洛谷P1914 小书童——凯撒密码c++
数据结构·c++·算法
BUG 劝退师1 小时前
八大经典排序算法
数据结构·算法·排序算法
m0_748240912 小时前
SpringMVC 请求参数接收
前端·javascript·算法
小林熬夜学编程2 小时前
【MySQL】第八弹---全面解析数据库表的增删改查操作:从创建到检索、排序与分页
linux·开发语言·数据库·mysql·算法
小小小白的编程日记2 小时前
List的基本功能(1)
数据结构·c++·算法·stl·list
_Itachi__2 小时前
LeetCode 热题 100 283. 移动零
数据结构·算法·leetcode
柃歌2 小时前
【UCB CS 61B SP24】Lecture 5 - Lists 3: DLLists and Arrays学习笔记
java·数据结构·笔记·学习·算法
鱼不如渔3 小时前
leetcode刷题第十三天——二叉树Ⅲ
linux·算法·leetcode
qwy7152292581633 小时前
10-R数组
python·算法·r语言
月上柳梢头&3 小时前
[C++ ]使用std::string作为函数参数时的注意事项
开发语言·c++·算法