[LeetCode] 1905. Count Sub Islands

You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land). An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.

An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.

Return the number of islands in grid2 that are considered sub-islands.

Example 1:
Example 1
Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
Output: 3
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.

Example 2:
Example 2
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
Output: 2
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.

Constraints:
m == grid1.length == grid2.length
n == grid1[i].length == grid2[i].length
1 <= m, n <= 500
grid1[i][j] and grid2[i][j] are either 0 or 1.

统计子岛屿。

给你两个 m x n 的二进制矩阵 grid1 和 grid2 ,它们只包含 0 (表示水域)和 1 (表示陆地)。一个 岛屿 是由 四个方向 (水平或者竖直)上相邻的 1 组成的区域。任何矩阵以外的区域都视为水域。

如果 grid2 的一个岛屿,被 grid1 的一个岛屿 完全 包含,也就是说 grid2 中该岛屿的每一个格子都被 grid1 中同一个岛屿完全包含,那么我们称 grid2 中的这个岛屿为 子岛屿 。

请你返回 grid2 中 子岛屿 的 数目 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-sub-islands
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

这是一道典型的 flood fill 类型的题,做法也类似于 200 题。我给出 DFS 和 BFS 的做法。

题目问的是两个尺寸相同的矩阵 grid1 和 grid2,在 grid2 中出现的岛屿是否能被 grid1 中的岛屿完全包含。按照这个题意,我们先去 grid2 里找一个岛屿的起点,在将当前这个岛的尺寸扩张的同时,我们需要判断所有在 grid2 里是岛屿的坐标,在 grid1 里是否也是岛屿,如果不是则返回 false。只有 grid2 遍历到的所有的 1 可以完全被 grid1 覆盖,才是子岛屿。

复杂度

时间O(mn)
空间O(n)

BFS实现

Java实现

class Solution {
    int[][] grid1;
    int[][] grid2;
    int m;
    int n;
    int[] dx = {-1, 1, 0, 0};
    int[] dy = {0, 0, -1, 1};

    public int countSubIslands(int[][] grid1, int[][] grid2) {
        this.grid1 = grid1;
        this.grid2 = grid2;
        m = grid1.length;
        n = grid1[0].length;
        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid2[i][j] == 1 && bfs(i, j)) {
                    count++;
                }
            }
        }
        return count;
    }

    private boolean bfs(int i, int j) {
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[] { i, j });
        grid2[i][j] = 0;
        boolean flag = true;
        if (grid1[i][j] == 0) {
            flag = false;
        }
        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int x = cur[0];
            int y = cur[1];
            for (int k = 0; k < 4; k++) {
                int newX = x + dx[k];
                int newY = y + dy[k];
                if (newX >= 0 && newX < m && newY >= 0 && newY < n && grid2[newX][newY] == 1) {
                    queue.offer(new int[] { newX, newY });
                    grid2[newX][newY] = 0;
                    if (grid1[newX][newY] == 0) {
                        flag = false;
                    }
                }
            }
        }
        return flag;
    }
}

DFS实现

Java实现

class Solution {
    int m;
    int n;

    public int countSubIslands(int[][] grid1, int[][] grid2) {
        m = grid2.length;
        n = grid2[0].length;
        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid2[i][j] == 1) {
                    if (dfs(grid1, grid2, i, j)) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    private boolean dfs(int[][] grid1, int[][] grid2, int i, int j) {
		// 如果没越界且这个坐标不是没访问过的陆地,则返回true
        if (i < 0 || j < 0 || i >= m || j >= n || grid2[i][j] != 1) {
            return true;
        }
        if (grid1[i][j] != grid2[i][j]) {
            return false;
        }
		// 标记一下,表示访问过这个点了
        grid2[i][j] = 2;
        boolean up = dfs(grid1, grid2, i - 1, j);
        boolean down = dfs(grid1, grid2, i + 1, j);
        boolean left = dfs(grid1, grid2, i, j - 1);
        boolean right = dfs(grid1, grid2, i, j + 1);
        return up && down && left && right;
    }
}
posted @ 2021-07-05 15:01  CNoodle  阅读(326)  评论(0编辑  收藏  举报