算法训练营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;
}
相关推荐
imuliuliang7 小时前
关于数据结构在算法设计中的核心作用解析7
算法
2401_894915539 小时前
GEO 搜索优化完整源码从零部署:环境配置、集群搭建全流程
开发语言·python·tcp/ip·算法·unity
普通网友10 小时前
共识算法实现:从工作量证明到权益证明的演进
算法·区块链·共识算法
ldmd28411 小时前
地图生成算法(噪声篇-Perlin,Simplex,Value noise)
算法·go·地图生成
国科安芯12 小时前
FreeRTOS RISC-V 浮点上下文切换移植:在 IAR 工程中完整保存 FPU 寄存器
java·开发语言·单片机·嵌入式硬件·算法·系统架构·risc-v
小陈phd12 小时前
集成检索介绍
算法
小poop12 小时前
轮转数组:从暴力到最优,一题掌握算法复杂度分析
数据结构·算法·leetcode
樱桃读报僵尸12 小时前
说说 LLMRouter,Agent 执行过程中怎么动态的选择 LLM
算法
gis开发之家13 小时前
《Vue3 从入门到大神40篇》Vue3 源码详解(十):diff 算法全解析 —— 为什么 Vue3 比 Vue2 更快?
javascript·算法·typescript·前端框架·vue3·vue3源码