算法训练营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;
}
相关推荐
晨非辰21 分钟前
【数据结构入坑指南】--《层序分明:堆的实现、排序与TOP-K问题一站式攻克(源码实战)》
c语言·开发语言·数据结构·算法·面试
hansang_IR30 分钟前
【题解】P2217 [HAOI2007] 分割矩阵 [记忆化搜索]
c++·数学·算法·记忆化搜索·深搜
Voyager_42 小时前
算法学习记录03——二叉树学习笔记:从两道题看透后序位置的关键作用
笔记·学习·算法
我搞slam7 小时前
快乐数--leetcode
算法·leetcode·哈希算法
WWZZ20258 小时前
快速上手大模型:机器学习3(多元线性回归及梯度、向量化、正规方程)
人工智能·算法·机器学习·机器人·slam·具身感知
东方佑9 小时前
从字符串中提取重复子串的Python算法解析
windows·python·算法
西阳未落9 小时前
LeetCode——二分(进阶)
算法·leetcode·职场和发展
通信小呆呆9 小时前
以矩阵视角统一理解:外积、Kronecker 积与 Khatri–Rao 积(含MATLAB可视化)
线性代数·算法·matlab·矩阵·信号处理
CoderCodingNo10 小时前
【GESP】C++四级真题 luogu-B4068 [GESP202412 四级] Recamán
开发语言·c++·算法
一个不知名程序员www10 小时前
算法学习入门---双指针(C++)
c++·算法