代码随想录训练营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;
}
相关推荐
山登绝顶我为峰 3(^v^)334 分钟前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
Two_brushes.2 小时前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先
森焱森4 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
QuantumStack6 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
写个博客6 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠6 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
hie988947 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
杰克尼7 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
.30-06Springfield8 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦8 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt