【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;
}
相关推荐
冲帕Chompa几秒前
图论part09dijkstra算法
算法·图论
·云扬·9 分钟前
【PmHub后端篇】PmHub中基于Redis加Lua脚本的计数器算法限流实现
redis·算法·lua
周Echo周11 分钟前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
zkmall15 分钟前
推荐算法工程化:ZKmall模板商城的B2C 商城的用户分层推荐策略
算法·机器学习·推荐算法
矿渣渣42 分钟前
AFFS2 的 `yaffs_ext_tags` 数据结构详解
数据结构·算法·文件系统·yaffs2
workflower1 小时前
使用谱聚类将相似度矩阵分为2类
人工智能·深度学习·算法·机器学习·设计模式·软件工程·软件需求
cwywsx1 小时前
Linux:进程控制2
linux·运维·算法
真的想上岸啊1 小时前
c语言第一个小游戏:贪吃蛇小游戏06
c语言·算法·链表
边跑边掩护1 小时前
LeetCode 648 单词替换题解
算法·leetcode·职场和发展
小森77672 小时前
(七)深度学习---神经网络原理与实现
人工智能·深度学习·神经网络·算法