当前位置: 首页 > news >正文

【Day44 LeetCode】图论问题 Ⅱ

一、图论问题 Ⅱ

1、岛屿的最大面积

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

# 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;
}

广度优先搜索代码如下:

# 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、孤岛总面积

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

# 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;
}

广度优先搜索代码如下:

# 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;
}

相关文章:

  • javaSE学习笔记23-线程(thread)-总结
  • 伪类选择器
  • 修改项目的一些前端记录(自用)
  • JavaScript中判断元素是否在可视区域内
  • linux有名管道的文件描述符3和4
  • 个人简历html网页模板,科技感炫酷html简历模板
  • DeepSeek API调用 Python
  • Hive中的分区和桶的概念及其作用
  • 网络工程师 (47)QOS
  • 小怿学习日记(七) | Unreal引擎灯光架构
  • 【wrk】wrk 压测工具入门
  • DeepSeek与ChatGPT的对比分析
  • K8s 之端口暴露(The Port of K8s is Exposed)
  • Jmeter连接数据库、逻辑控制器、定时器
  • SOME/IP--协议英文原文讲解8
  • 《魔女的夜宴》无广版手游安卓苹果免费下载直装版
  • 红蓝对抗之常见网络安全事件研判、了解网络安全设备、Webshell入侵检测
  • Mac端homebrew安装配置
  • P1115 最大子段和
  • fps僵尸:8.丧尸死亡
  • 外国游客“在华扫货”热:“带空箱子到中国!”
  • 真人秀《幸存者》百万美元奖金,25年间“缩水”近一半
  • 江西省市场监管局原局长谢来发被双开:违规接受旅游活动安排
  • 韩国总统选战打响:7人角逐李在明领跑,执政党临阵换将陷入分裂
  • 93岁南开退休教授陈生玺逝世,代表作《明清易代史独见》多次再版
  • 退休10年后,70岁成都高新区管委会原巡视员王晋成被查