算法训练营day66-孤岛总面积-沉没孤岛-水流问题-建造最大岛屿

题目1:101. 孤岛的总面积 (kamacoder.com)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};
int count = 0;
void bfs(vector<vector<int>>& map, vector<vector<bool>>& vistied, int x, int y, bool flag) {
    queue<pair<int, int>> qu;
    qu.push(pair<int, int>(x, y));
    vistied[x][y] = true;
    if(flag) map[x][y] = 0;
    else count++;
    while(!qu.empty()) {
        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(!vistied[nextx][nexty] && map[nextx][nexty] == 1) {
                vistied[nextx][nexty] = true;
                qu.push(pair<int, int>(nextx, nexty));
                if(flag) map[nextx][nexty] = 0;
                else count++;
            }
        }
    }
}

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]; 
        }
    }
    
    vector<vector<bool>> vistied(n, vector<bool>(m, false));
    for(int j = 0;j < m;j++) {
        if(map[0][j] == 1 && !vistied[0][j]) bfs(map, vistied, 0, j, true);
        if(map[n - 1][j] == 1 && !vistied[n - 1][j]) bfs(map, vistied, n - 1, j, true);
    }
    for(int i = 0;i < n;i++) {
        if(map[i][0] == 1 && !vistied[i][0]) bfs(map, vistied, i, 0, true);
        if(map[i][m - 1] == 1 && !vistied[i][m - 1]) bfs(map, vistied, i, m - 1, true);
    }

    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(!vistied[i][j] && map[i][j] == 1) {
                // if(i != 0 && i != n - 1 && j != 0 && j != m - 1) {
                //     result++;
                // }
                bfs(map,vistied,i,j, false);
            }
        }
    }
    cout << count << endl;
    return 0;
}

题目2:102. 沉没孤岛 (kamacoder.com)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};

void bfs(vector<vector<int>>& map, vector<vector<bool>>& vistied, vector<vector<int>>& result, int x, int y, bool flag) {
    queue<pair<int, int>> qu;
    qu.push(pair<int, int>(x, y));
    vistied[x][y] = true;
    if(flag) map[x][y] = 0;
    else result[x][y] = 0;
    while(!qu.empty()) {
        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(!vistied[nextx][nexty] && map[nextx][nexty] == 1) {
                vistied[nextx][nexty] = true;
                qu.push(pair<int, int>(nextx, nexty));
                if(flag) map[nextx][nexty] = 0;
                else result[nextx][nexty] = 0;
            }
        }
    }
}

int main() {
    int n,m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m));
    vector<vector<int>> result(n, vector<int>(m));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j];
            result[i][j] = map[i][j];
        }
    }
    
    vector<vector<bool>> vistied(n, vector<bool>(m, false));
    for(int j = 0;j < m;j++) {
        if(map[0][j] == 1 && !vistied[0][j]) bfs(map, vistied, result, 0, j, true);
        if(map[n - 1][j] == 1 && !vistied[n - 1][j]) bfs(map, vistied, result, n - 1, j, true);
    }
    for(int i = 0;i < n;i++) {
        if(map[i][0] == 1 && !vistied[i][0]) bfs(map, vistied, result, i, 0, true);
        if(map[i][m - 1] == 1 && !vistied[i][m - 1]) bfs(map, vistied, result, i, m - 1, true);
    }

    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(!vistied[i][j] && map[i][j] == 1) {
                bfs(map,vistied, result,i,j, false);
            }
        }
    }
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m - 1;j++) {
             cout << result[i][j] << ' ';
        }
        cout << result[i][m - 1] << endl;
    }
   
    return 0;
}

题目3:103. 水流问题 (kamacoder.com)

题目4:104. 建造最大岛屿 (kamacoder.com)

相关推荐
灭霸11238 分钟前
力扣 用队列实现栈(Java)
算法·leetcode·职场和发展
-代号952740 分钟前
【LeetCode】十、二分查找法:寻找峰值 + 二维矩阵的搜索
算法·leetcode·矩阵
@干吧jyb1 小时前
贪心算法练习题(7/2)
数据结构·算法·leetcode·贪心算法
A22742 小时前
LeetCode 1327, 383, 236
java·算法·leetcode
jiayoushijie-泽宣2 小时前
深入浅出3D感知中的优化与基于学习的技术 (第三章) 原创教程
人工智能·算法·机器学习·3d·机器人
小oo呆4 小时前
【机器学习300问】135、决策树算法ID3的局限性在哪儿?C4.5算法做出了怎样的改进?
算法·决策树·机器学习
情系明明5 小时前
使用c++设计一个计算器
数据结构·c++·算法
神州问学6 小时前
存算一体架构或成为AI处理器技术发展关键
人工智能·算法·语言模型·架构·gpu算力
哥廷根数学学派8 小时前
基于自组织长短期记忆神经网络的时间序列预测(MATLAB)
开发语言·人工智能·深度学习·神经网络·算法·机器学习·matlab
pkukevin10 小时前
基于文本密度的网页正文提取的研究与实现
算法·html