算法训练营day65-岛屿连同问题

深搜-岛屿:99. 岛屿数量 (kamacoder.com)

复制代码
// 深度搜索 dfs
#include<bits/stdc++.h>

using namespace std;

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

void dfs(vector<vector<int>>& map, vector<vector<bool>>& findednode, int newx, int newy) {
    if(map[newx][newy] == 0 || findednode[newx][newy] == true) return;
    findednode[newx][newy] = true;
    for(int i = 0;i < 4;i++) {
        int nextx = newx + dir[i][0];
        int nexty = newy + dir[i][1];
        if(nextx < 0 || nextx >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;
        dfs(map, findednode, nextx, nexty);
    }
}
 
int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j];
        }
    }
    int result = 0;
    vector<vector<bool>> findednode(n, vector<bool>(m, false));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(map[i][j] == 1 && findednode[i][j] == false) {
                result++;
                dfs(map, findednode, i, j);
            }
        }
    }
    cout << result << endl;
    return 0;
}

// 深度搜索 dfs 递归终止条件写在for里面了
#include<bits/stdc++.h>

using namespace std;

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

void dfs(vector<vector<int>>& map, vector<vector<bool>>& findednode, int newx, int newy) {
    findednode[newx][newy] = true;
    for(int i = 0;i < 4;i++) {
        int nextx = newx + dir[i][0];
        int nexty = newy + dir[i][1];
        if(nextx < 0 || nextx >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;
        if(findednode[nextx][nexty] != true && map[nextx][nexty] == 1) {
            dfs(map, findednode, nextx, nexty);
        }
        
    }
}
 
int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j];
        }
    }
    int result = 0;
    vector<vector<bool>> findednode(n, vector<bool>(m, false));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(map[i][j] == 1 && findednode[i][j] == false) {
                result++;
                dfs(map, findednode, i, j);
            }
        }
    }
    cout << result << endl;
    return 0;
}

广搜-岛屿:99. 岛屿数量 (kamacoder.com)

复制代码
// 深度搜索 dfs
#include<bits/stdc++.h>

using namespace std;

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

void bfs(vector<vector<int>>& map, vector<vector<bool>>& findednode, int newx, int newy) {
    queue<pair<int, int>> qu;
    qu.push(pair<int,int>(newx, newy));
    findednode[newx][newy] = true;
    while(!qu.empty()) {
        pair<int, int>cur = qu.front();
        qu.pop();
        int x = cur.first;
        int y = cur.second;
        for(int i = 0;i < 4;i++) {
            int nextx = x + dir[i][0];
            int nexty = y + dir[i][1];
            if(nextx < 0 || nextx >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;
            if(findednode[nextx][nexty] != true && map[nextx][nexty] == 1) {
                qu.push(pair<int, int>(nextx, nexty));
                findednode[nextx][nexty] = true;
            }
        }
    }
}
 
int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j];
        }
    }
    int result = 0;
    vector<vector<bool>> findednode(n, vector<bool>(m, false));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(map[i][j] == 1 && findednode[i][j] == false) {
                result++;
                bfs(map, findednode, i, j);
            }
        }
    }
    cout << result << endl;
    return 0;
}

题目:100. 岛屿的最大面积 (kamacoder.com)

复制代码
#include<bits/stdc++.h>

using namespace std;
int size = 0;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};

void bfs(vector<vector<int>>& map, vector<vector<bool>>& visited, int x, int y) {
    queue<pair<int, int>> qu;
    qu.push(pair<int, int>(x, y));
    visited[x][y] = true;
    while(!qu.empty()) {
        size++;
        pair<int, int> cur = qu.front();
        qu.pop();
        int newx = cur.first;
        int newy = cur.second;
        for(int i = 0;i < 4;i++) {
            int nextx = newx + dir[i][0];
            int nexty = newy + dir[i][1];
            if(nextx < 0 || nextx >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;
            if(!visited[nextx][nexty] && map[nextx][nexty] == 1) {
                visited[nextx][nexty] = true;
                qu.push(pair<int, int>(nextx, nexty));
            }
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m , 0));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j];
        }
    }
    vector<vector<bool>> visited(n, vector<bool>(m , false));
    int result = 0;
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(map[i][j] == 1 && !visited[i][j]) {
                size = 0;
                bfs(map, visited, i, j);
                result = max(result, size);
            }
        }
    }
    cout << result << endl;
    return 0;
}
相关推荐
迈巴赫车主1 小时前
蓝桥杯 20541魔法科考试
java·数据结构·算法·蓝桥杯
star learning white1 小时前
xmC语言8
c语言·开发语言·算法
青小俊2 小时前
【代码随想录c++刷题】-二分查找 移除元素 有序数组的平方 - 第一章 数组 part 01
c++·算法·leetcode
ytttr8732 小时前
基于MATLAB实现晶体共晶凝固模拟
开发语言·算法·matlab
倦王3 小时前
力扣日刷251120
算法·leetcode·职场和发展
F_D_Z3 小时前
【k近邻】Kd树构造与最近邻搜索示例
算法·机器学习·近邻算法·k近邻算法
断剑zou天涯3 小时前
【算法笔记】从暴力递归到动态规划(二)
java·算法·动态规划
RTC老炮3 小时前
webrtc降噪-SpeechProbabilityEstimator类源码分析与算法原理
算法·webrtc
WWZZ20253 小时前
快速上手大模型:深度学习9(池化层、卷积神经网络1)
人工智能·深度学习·神经网络·算法·机器人·大模型·具身智能
Boop_wu3 小时前
[Java EE] 多线程编程初阶
java·jvm·算法