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

岛屿数量

卡码网题目链接

c 复制代码
#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;
}

深搜

c 复制代码
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);
    }
}

广搜

c 复制代码
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模式)

c 复制代码
#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;
}
相关推荐
pystraf13 分钟前
UOJ 228 基础数据结构练习题 Solution
数据结构·c++·算法·线段树
海底火旺22 分钟前
破解二维矩阵搜索难题:从暴力到最优的算法之旅
javascript·算法·面试
黄昏ivi1 小时前
电力系统最小惯性常数解析
算法
独家回忆3642 小时前
每日算法-250425
算法
烁3472 小时前
每日一题(小白)模拟娱乐篇33
java·开发语言·算法
Demons_kirit2 小时前
LeetCode 2799、2840题解
算法·leetcode·职场和发展
软行2 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
永远在Debug的小殿下2 小时前
查找函数【C++】
数据结构·算法
我想进大厂3 小时前
图论---染色法(判断是否为二分图)
数据结构·c++·算法·深度优先·图论
油泼辣子多加3 小时前
【风控】稳定性指标PSI
人工智能·算法·金融