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

代码随想录训练营day51|图论part2

岛屿数量

卡码网题目链接

#include <bits/stdc++.h>
using namespace std;

int dir[4][2] = {0,-1,0,1,1,0,-1,0};

void dfs(vector<vector<int>> &board, vector<vector<bool>> &visted, int x, int y){
    for(int i = 0; i < 4; i++){
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if(nx >= board.size() || nx < 0 || ny >= board[0].size() || ny < 0 || board[nx][ny] == 0 || visted[nx][ny])
            continue;
        // board[nx][ny] = 0;
        visted[nx][ny] = true;
        dfs(board, visted, nx, ny);
    }
}

void bfs(vector<vector<int>> &board, vector<vector<bool>> &visted, int x, int y){
    queue<pair<int, int>> que;
    que.push({x, y});
    while(!que.empty()){
        pair<int, int> s = que.front();
        que.pop();
        for(int i = 0; i < 4; i++){
            int nx = s.first + dir[i][0];
            int ny = s.second + dir[i][1];
            if(nx >= board.size() || nx < 0 || ny >= board[0].size() || ny < 0 || board[nx][ny] == 0 || visted[nx][ny])
                continue;
            visted[nx][ny] = true;
            que.push({nx, ny});
        }
    }

}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> board(n, vector<int>(m));
    vector<vector<bool>> visted(n, vector<bool>(m, false));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> board[i][j];
        }
    }
    int result = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(board[i][j] == 1 && !visted[i][j]){
                result++;
                visted[i][j] = true;
                // dfs(board, visted, i, j);
                bfs(board, visted, i, j);
            }
        }
    }
    cout << result << endl;
}

深搜

int dir[4][2] = {0,-1,0,1,1,0,-1,0};
void dfs(vector<vector<int>> &board, vector<vector<bool>> &visted, int x, int y){
    for(int i = 0; i < 4; i++){
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if(nx >= board.size() || nx < 0 || ny >= board[0].size() || ny < 0 || board[nx][ny] == 0 || visted[nx][ny])
            continue;
        // board[nx][ny] = 0;
        visted[nx][ny] = true;
        dfs(board, visted, nx, ny);
    }
}



广搜

int dir[4][2] = {0,-1,0,1,1,0,-1,0};
void bfs(vector<vector<int>> &board, vector<vector<bool>> &visted, int x, int y){
    queue<pair<int, int>> que;
    que.push({x, y});
    while(!que.empty()){
        pair<int, int> s = que.front();
        que.pop();
        for(int i = 0; i < 4; i++){
            int nx = s.first + dir[i][0];
            int ny = s.second + dir[i][1];
            if(nx >= board.size() || nx < 0 || ny >= board[0].size() || ny < 0 || board[nx][ny] == 0 || visted[nx][ny])
                continue;
            visted[nx][ny] = true;
            que.push({nx, ny});
        }
    }
}

岛屿的最大面积

卡码网题目链接(ACM模式)

#include <bits/stdc++.h>
using namespace std;

int result;
int dir[4][2] = {0,-1,0,1,1,0,-1,0};
void bfs(vector<vector<int>> &board, vector<vector<bool>> &visted, int x, int y){
    queue<pair<int, int>> que;
    que.push({x, y});
    int sum = 1;
    while(!que.empty()){
        pair<int, int> s = que.front();
        que.pop();
        for(int i = 0; i < 4; i++){
            int nx = s.first + dir[i][0];
            int ny = s.second + dir[i][1];
            if(nx >= board.size() || nx < 0 || ny >= board[0].size() || ny < 0 || board[nx][ny] == 0 || visted[nx][ny])
                continue;
            sum++;
            visted[nx][ny] = true;
            que.push({nx, ny});
        }
    }
    result = max(sum, result);
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> board(n, vector<int>(m));
    vector<vector<bool>> visted(n, vector<bool>(m, false));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> board[i][j];
        }
    }
    result = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(board[i][j] == 1 && !visted[i][j]){
                visted[i][j] = true;
                // result++;
                bfs(board, visted, i, j);
            }
        }
    }
    cout << result;
}

http://www.kler.cn/news/289059.html

相关文章:

  • 解锁.NET安全奥秘:敏感数据加密与哈希的深度揭秘
  • C++系列-const所有用法总结
  • HALCON 错误代码 #7709
  • python-简单的dos攻击
  • 第十四章 rust集合库介绍
  • Mybatis【分页插件,缓存,一级缓存,二级缓存,常见缓存面试题】
  • 【HarmonyOS】模仿个人中心头像图片,调用系统相机拍照,从系统相册选择图片和圆形裁剪显示 (二)
  • 免费升级https访问
  • Vue3 reactive和ref
  • Chapter 07 watch侦听器
  • 【Next】1. 初识服务端渲染
  • OpenHarmony如何切换横竖屏?
  • 科研绘图系列:R语言富集火山图和通路图(volcano plot pathway)
  • 实现流程化办公,可专注于开源可视化报表设计器!
  • F12抓包01:启动、面板功能介绍、语言设置、前端样式调试
  • 【#第三期实战营闯关作业 ## MindSearch在 Hugging FaceSpace的部署】
  • 缓存解决方案。Redis 和 Amazon ElastiCache 比较
  • lit-llama代码解析
  • 【C++ 面试 - STL】每日 3 题(五)
  • 解读GaussianTalker:利用音频驱动的基于3D高斯点染技术的实时高保真讲话头像合成
  • Idea_服务器自动化部署_傻瓜式教程
  • MySQL中的分组统计
  • 云计算环境下的数据治理
  • 学习之git
  • 算法设计:实验二贪心算法
  • wget下载速度受到哪些因素影响?
  • MySQL:简述多版本并发控制MVCC
  • 无人机之电池篇
  • Python与R的完美协作:深入解析subprocess模块调用R脚本的参数传递机制
  • 安装WMware和Ubuntu并使用xShell连接