*算法训练(leetcode)第四十四天 | 99. 岛屿数量、100. 岛屿的最大面积

*99. 岛屿数量

题目地址

DFS

深度优先遍历。类似于找连通分量。

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

// c++
#include<bits/stdc++.h>
using namespace std;
void dfs(const vector<vector<int>> &matrix, 
                vector<vector<bool>> &visited,
                int x, int y){
    // 上下左右 四个方向
    int direction[4][2] ={0, 1, 0, -1, -1, 0, 1, 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[0].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));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin>>matrix[i][j];
        }
    }
    int result = 0;
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(matrix[i][j] && !visited[i][j]){
                result++;
                visited[i][j] = true;
                dfs(matrix, visited, i, j);
            }
        }
    }
    cout<<result;
    return 0;
}

BFS

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

// c++
#include<bits/stdc++.h>
using namespace std;
void dfs(const vector<vector<int>> &matrix, 
                vector<vector<bool>> &visited,
                int x, int y){
    int direction[4][2] ={0, 1, 0, -1, 1, 0, -1, 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[0].size() || nextx<0 || nexty<0) continue;
        if(matrix[nextx][nexty] && !visited[nextx][nexty]){
            visited[nextx][nexty] = true;
            dfs(matrix, visited, nextx, nexty);
        }
    }
    
}
void bfs(const vector<vector<int>> &matrix, 
                vector<vector<bool>> &visited,
                int x, int y){
    queue<pair<int, int>> que;
    que.push({x, y});
    visited[x][y] = true;
    int direction[4][2] ={0, 1, 0, -1, 1, 0, -1, 0};
    
    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] && !visited[nextx][nexty]){
                que.push({nextx, nexty});
                visited[nextx][nexty] = true;
            }
        }
        
        
    }
                    
}
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];
        }
    }
    int result = 0;
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(matrix[i][j] && !visited[i][j]){
                result++;
                // visited[i][j] = true;
                // dfs(matrix, visited, i, j);
                bfs(matrix, visited, i, j);
            }
        }
    }
    
    
    cout<<result;
    return 0;
}

100. 岛屿的最大面积

题目地址

DFS

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

// c++
#include<bits/stdc++.h>
using namespace std;

int direction[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void dfs(const vector<vector<int>> &matrix, 
            vector<vector<bool>> &visited,
            int x, int y,
            int &cnt){
    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(!visited[nextx][nexty] && matrix[nextx][nexty]){
            cnt++;
            visited[nextx][nexty] = true;
            dfs(matrix, visited, nextx, nexty, cnt);
        }
    }            
}

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];
        }
    }
    int result = 0;
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(!visited[i][j] && matrix[i][j]){
                int cnt = 1;
                visited[i][j] = true;
                dfs(matrix, visited, i, j, cnt);
                if(cnt > result) result = cnt;
            }
        }
    }
    cout<<result;
    return 0;
}

BFS

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

//c++
#include<bits/stdc++.h>
using namespace std;

int direction[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void dfs(const vector<vector<int>> &matrix, 
            vector<vector<bool>> &visited,
            int x, int y,
            int &cnt){
    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(!visited[nextx][nexty] && matrix[nextx][nexty]){
            cnt++;
            visited[nextx][nexty] = true;
            dfs(matrix, visited, nextx, nexty, cnt);
        }
    }            
}

void bfs(const vector<vector<int>> &matrix, 
            vector<vector<bool>> &visited,
            int x, int y,
            int &cnt){
    
    queue<pair<int, int>> que;
    que.push({x, y});
    visited[x][y] = true;
    
    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(!visited[nextx][nexty] && matrix[nextx][nexty]){
                cnt++;
                visited[nextx][nexty] = true;
                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];
        }
    }
    int result = 0;
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(!visited[i][j] && matrix[i][j]){
                int cnt = 1;
                // visited[i][j] = true;
                // dfs(matrix, visited, i, j, cnt);
                bfs(matrix, visited, i, j, cnt);
                if(cnt > result) result = cnt;
            }
        }
    }
    
    
    cout<<result;
    return 0;
}

  • 43
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值