zoukankan      html  css  js  c++  java
  • 1905. 统计子岛屿

    遍历grid2,如果在遍历一个岛屿的过程中没有超过相应的grid1的1的范围,说明遍历到了一个子岛屿,否则不是子岛屿

    int st[510][510];
    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    int valid;
    class Solution { 
    public:
        void dfs(int x, int y, int row, int col, vector<vector<int>>& grid1, vector<vector<int>>& grid2, int &valid){
            st[x][y] = 1;
            if(grid1[x][y] == 0) valid = 0;
            for(int i = 0; i < 4; i ++){
                int a = x + dx[i], b = y + dy[i];
                if(a < 0 || a >= row || b < 0 || b >= col) continue;
                if(grid2[a][b] && !st[a][b]) dfs(a, b, row, col, grid1, grid2, valid);
            }
        }
        
        int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
            memset(st, 0, sizeof st);
            int row = grid1.size(), col = grid1[0].size();
            int cnt = 0;
            for(int i = 0; i < row; i ++){
                for(int j = 0; j < col; j ++){
                    if(grid2[i][j] && st[i][j] == 0){
                        valid = 1;
                        dfs(i, j, row, col, grid1, grid2, valid);
                        cnt += valid;
                    }
                }
            }
    
            return cnt;
        }
    };
    
  • 相关阅读:
    Python并发编程之多进程(实战)
    ThreadPoolExecutor源码分析
    JDK 1.8 JVM的变化
    JDK1.8 hashMap源码分析
    Spring解决循环依赖
    spring
    实现一个可重入锁和不可重入锁
    B树与B+树
    WebMagic
    Java高频面试题
  • 原文地址:https://www.cnblogs.com/tomori/p/15172767.html
Copyright © 2011-2022 走看看