*算法训练(leetcode)第四十五天 | 101. 孤岛的总面积、102. 沉没孤岛、103. 水流问题、104. 建造最大岛屿

刷题记录

  • [101. 孤岛的总面积](#101. 孤岛的总面积)
  • [102. 沉没孤岛](#102. 沉没孤岛)
  • [*103. 水流问题](#*103. 水流问题)
  • [*104. 建造最大岛屿](#*104. 建造最大岛屿)

101. 孤岛的总面积

题目地址

本题要求不与矩阵边缘相连的孤岛的总面积。先将与四个边缘相连的岛屿变为海洋,再统计剩余的孤岛的总面积。无需再标识访问过的结点,因为访问过后都变为海洋了。

时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n 2 ) O(n^2) O(n2)

DFS

cpp 复制代码
// c++
#include<bits/stdc++.h>
using namespace std;
int direction[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int result = 0;

void pre_dfs(vector<vector<int>> &matrix, int x, int  y){
    
    matrix[x][y] = 0;
    result++;
    for(int i=0; i<4; i++){
        int nextx = x + direction[i][0];
        int nexty = y + direction[i][1];
        if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
        if(matrix[nextx][nexty]){
            matrix[nextx][nexty] = 0;
            
            pre_dfs(matrix, nextx, nexty);
        }
        
    }
}

int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> matrix(n, vector<int>(m, 0));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin>>matrix[i][j];
        }
    }
    
    for(int i=0; i<n; i++) {
        if(matrix[i][0]){
            pre_dfs(matrix, i, 0);
        }
        if(matrix[i][m-1]){
            pre_dfs(matrix, i, m-1);
        }
    }
    for(int j=0; j<m; j++){
        if(matrix[0][j]){
            pre_dfs(matrix, 0, j);
        }
        if(matrix[n-1][j]){
            pre_dfs(matrix, n-1, j);   
        }
       
    }
    result = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(matrix[i][j]){
                pre_dfs(matrix, i, j);   
            }
        }
    }
    cout<<result;
    return 0;
}

BFS

cpp 复制代码
// c++
#include<bits/stdc++.h>
using namespace std;
int direction[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int result = 0;

void pre_dfs(vector<vector<int>> &matrix, int x, int  y){
    
    matrix[x][y] = 0;
    result++;
    for(int i=0; i<4; i++){
        int nextx = x + direction[i][0];
        int nexty = y + direction[i][1];
        if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
        if(matrix[nextx][nexty]){
            matrix[nextx][nexty] = 0;
            
            pre_dfs(matrix, nextx, nexty);
        }
        
    }
}

void pre_bfs(vector<vector<int>> &matrix, int x, int  y){
    queue<pair<int, int>> que;
    que.push({x, y});
    matrix[x][y] = 0;
    result++;
    while(!que.empty()){
        pair<int, int> cur = que.front();
        que.pop();
        int curx = cur.first;
        int cury = cur.second;
        for(int i=0; i<4; i++){
            int nextx = curx + direction[i][0];
            int nexty = cury + direction[i][1];
            if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
            if(matrix[nextx][nexty]){
                matrix[nextx][nexty] = 0;
                result++;
                que.push({nextx, nexty});
            }
        }
        
    }
}

int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> matrix(n, vector<int>(m, 0));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin>>matrix[i][j];
        }
    }
    /*
    // dfs
    for(int i=0; i<n; i++) {
        if(matrix[i][0]){
            pre_dfs(matrix, i, 0);
        }
        if(matrix[i][m-1]){
            pre_dfs(matrix, i, m-1);
        }
    }
    for(int j=0; j<m; j++){
        if(matrix[0][j]){
            pre_dfs(matrix, 0, j);
        }
        if(matrix[n-1][j]){
            pre_dfs(matrix, n-1, j);   
        }
       
    }
    */
    // bfs
    for(int i=0; i<n; i++) {
        if(matrix[i][0]){
            pre_bfs(matrix, i, 0);
        }
        if(matrix[i][m-1]){
            pre_bfs(matrix, i, m-1);
        }
    }
    for(int j=0; j<m; j++){
        if(matrix[0][j]){
            pre_bfs(matrix, 0, j);
        }
        if(matrix[n-1][j]){
            pre_bfs(matrix, n-1, j);   
        }
       
    }
    result = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(matrix[i][j]){
                // pre_dfs(matrix, i, j);   
                pre_bfs(matrix, i, j);
            }
        }
    }
    cout<<result;
    return 0;
}

102. 沉没孤岛

题目地址

本题是上一题的反向操作

先把非孤岛做访问标记,再对剩余陆地进行操作。

时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n 2 ) O(n^2) O(n2)

DFS

cpp 复制代码
// c++
#include<bits/stdc++.h>
using namespace std;
int direction[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void pre_dfs(const vector<vector<int>>& matrix,
            vector<vector<bool>>& visited,
            int x, int y){
    visited[x][y] = true;
    for(int i=0; i<4; i++){
        int nextx = x + direction[i][0];
        int nexty = y + direction[i][1];
        if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;
        if(matrix[nextx][nexty] && !visited[nextx][nexty]){
            // visited[nextx][nexty] = true;
            pre_dfs(matrix, visited, nextx, nexty);
        }
    }
}

void dfs(vector<vector<int>>& matrix,
            vector<vector<bool>>& visited,
            int x, int y){
    matrix[x][y] = 0;
    for(int i=0; i<4; i++){
        int nextx = x + direction[i][0];
        int nexty = y + direction[i][1];
        if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;
        if(matrix[nextx][nexty] && !visited[nextx][nexty]){
            visited[nextx][nexty] = true;
            dfs(matrix, visited, nextx, nexty);
        }
    }
}
int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> matrix(n, vector<int>(m, 0));
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin>>matrix[i][j];
        }
    }
    for(int i=0; i<n; i++){
        if(matrix[i][0] && !visited[i][0]) pre_dfs(matrix, visited, i, 0);
        if(matrix[i][m-1] && !visited[i][m-1]) pre_dfs(matrix, visited, i, m-1);
    }
    
    for(int j=0; j<m; j++){
        if(matrix[0][j] && !visited[0][j]) pre_dfs(matrix, visited, 0, j);
        if(matrix[n-1][j] && !visited[n-1][j]) pre_dfs(matrix, visited, n-1, j);
    }
    
    for(int i=0; i<n; i++){
        
        
        for(int j=0; j<m; j++){
            if(matrix[i][j] && !visited[i][j]){
                visited[i][j] = true;
                dfs(matrix,visited, i, j);
            }
        }
        for(int j=0; j<m; j++) cout<<matrix[i][j]<<" ";
        cout<<endl;
        
    }
    
    
    
    
    return 0;
}

BFS

cpp 复制代码
//c++
#include<bits/stdc++.h>
using namespace std;
int direction[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void pre_dfs(const vector<vector<int>>& matrix,
            vector<vector<bool>>& visited,
            int x, int y){
    visited[x][y] = true;
    for(int i=0; i<4; i++){
        int nextx = x + direction[i][0];
        int nexty = y + direction[i][1];
        if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;
        if(matrix[nextx][nexty] && !visited[nextx][nexty]){
            // visited[nextx][nexty] = true;
            pre_dfs(matrix, visited, nextx, nexty);
        }
    }
}

void dfs(vector<vector<int>>& matrix,
            vector<vector<bool>>& visited,
            int x, int y){
    matrix[x][y] = 0;
    
    for(int i=0; i<4; i++){
        int nextx = x + direction[i][0];
        int nexty = y + direction[i][1];
        if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;
        if(matrix[nextx][nexty] && !visited[nextx][nexty]){
            visited[nextx][nexty] = true;
            dfs(matrix, visited, nextx, nexty);
        }
    }
}

void pre_bfs(const vector<vector<int>>& matrix,
            vector<vector<bool>>& visited,
            int x, int y){
    visited[x][y] = true;
    queue<pair<int, int>> que;
    que.push({x,y});
    while(!que.empty()){
        pair<int, int> cur = que.front();
        que.pop();
        int curx = cur.first;
        int cury = cur.second;
        for(int i=0; i<4; i++){
            int nextx = curx + direction[i][0];
            int nexty = cury + direction[i][1];
            if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;
            if(matrix[nextx][nexty] && !visited[nextx][nexty]){
                visited[nextx][nexty] = true;
                que.push({nextx, nexty});
            }
        }
    }
}

void bfs(vector<vector<int>>& matrix,
            vector<vector<bool>>& visited,
            int x, int y){
    visited[x][y] = true;
    matrix[x][y] = 0;
    queue<pair<int, int>> que;
    que.push({x,y});
    while(!que.empty()){
        pair<int, int> cur = que.front();
        que.pop();
        int curx = cur.first;
        int cury = cur.second;
        for(int i=0; i<4; i++){
            int nextx = curx + direction[i][0];
            int nexty = cury + direction[i][1];
            if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;
            if(matrix[nextx][nexty] && !visited[nextx][nexty]){
                visited[nextx][nexty] = true;
                matrix[nextx][nexty] = 0;
                que.push({nextx, nexty});
            }
        }
    }
}
int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> matrix(n, vector<int>(m, 0));
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin>>matrix[i][j];
        }
    }
    /*
    // dfs
    for(int i=0; i<n; i++){
        if(matrix[i][0] && !visited[i][0]) pre_dfs(matrix, visited, i, 0);
        if(matrix[i][m-1] && !visited[i][m-1]) pre_dfs(matrix, visited, i, m-1);
    }
    
    for(int j=0; j<m; j++){
        if(matrix[0][j] && !visited[0][j]) pre_dfs(matrix, visited, 0, j);
        if(matrix[n-1][j] && !visited[n-1][j]) pre_dfs(matrix, visited, n-1, j);
    }
    */
    // bfs
    for(int i=0; i<n; i++){
        if(matrix[i][0] && !visited[i][0]) pre_bfs(matrix, visited, i, 0);
        if(matrix[i][m-1] && !visited[i][m-1]) pre_bfs(matrix, visited, i, m-1);
    }
    
    for(int j=0; j<m; j++){
        if(matrix[0][j] && !visited[0][j]) pre_bfs(matrix, visited, 0, j);
        if(matrix[n-1][j] && !visited[n-1][j]) pre_bfs(matrix, visited, n-1, j);
    }
    
    for(int i=0; i<n; i++){
        
        
        for(int j=0; j<m; j++){
            if(matrix[i][j] && !visited[i][j]){
                // visited[i][j] = true;
                // dfs(matrix,visited, i, j);
                bfs(matrix,visited, i, j);
            }
        }
        for(int j=0; j<m; j++) cout<<matrix[i][j]<<" ";
        cout<<endl;
        
    }
    
    
    
    
    return 0;
}

*103. 水流问题

题目地址

使用两个标识访问的数组分别从两组边界出发进行dfs遍历,使用从低向高流(反向流)来分别记录两组边界的结点。最后两组边界的交集就是本题答案。
思路

时间复杂度: O ( m ∗ n ) O(m*n) O(m∗n)
空间复杂度: O ( m ∗ n ) O(m*n) O(m∗n)

cpp 复制代码
// c++
#include<bits/stdc++.h>
using namespace std;
int direction[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void dfs(const vector<vector<int>> &matrix, 
        vector<vector<bool>> &visited,
        int x, int y){
    visited[x][y] = true;
    
    for(int i=0; i<4; i++){
        int nextx = x + direction[i][0];
        int nexty = y + direction[i][1];
        if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
        if(matrix[x][y]>matrix[nextx][nexty]) continue;
        if(!visited[nextx][nexty]) dfs(matrix, visited, nextx, nexty);
    }
}

int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> matrix(n, vector<int>(m, 0));
    vector<vector<bool>> first(n, vector<bool>(m, false));
    vector<vector<bool>> second(n, vector<bool>(m, false));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin>>matrix[i][j];
        }
    }
    
    for(int i=0; i<n; i++){
        dfs(matrix, first, i, 0);
        dfs(matrix, second, i, m-1);
    }
    for(int j=0; j<m; j++){
        dfs(matrix, first, 0, j);
        dfs(matrix, second, n-1, j);
    }
    
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(first[i][j] && second[i][j]) cout<<i<<" "<<j<<endl;
        }
    }
    
    return 0;   
}

*104. 建造最大岛屿

题目地址

题解思路

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

cpp 复制代码
// c++
相关推荐
Paddi93039 分钟前
Codeforces Round 1004 (Div. 1) C. Bitwise Slides
c++·算法
Luis Li 的猫猫2 小时前
深度学习中的知识蒸馏
人工智能·经验分享·深度学习·学习·算法
查理零世2 小时前
【蓝桥杯集训·每日一题2025】 AcWing 6118. 蛋糕游戏 python
python·算法·蓝桥杯
敲代码的小王!2 小时前
MD5加密算法和BCrypt密码加密算法
java·算法·安全
带娃的IT创业者3 小时前
机器学习实战(6):支持向量机(SVM)——强大的非线性分类器
算法·机器学习·支持向量机
流星白龙5 小时前
【C++】36.C++IO流
开发语言·c++
孑么5 小时前
力扣 买卖股票的最佳时机
算法·leetcode·职场和发展·贪心算法·动态规划
诚信爱国敬业友善6 小时前
常见排序方法的总结归类
开发语言·python·算法
靡不有初1116 小时前
CCF-CSP第31次认证第二题——坐标变换(其二)【NA!前缀和思想的细节,输出为0的常见原因】
c++·学习·ccfcsp
gentle_ice7 小时前
跳跃游戏 II - 贪心算法解法
数据结构·算法·leetcode·贪心算法