友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

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:

1905 01
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:

1905 02
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.

思路分析

  • 一刷(沉岛)

  • 一刷(自己想法)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * 借鉴沉岛思路
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2024-07-12 14:51:03
 */
public int countSubIslands(int[][] grid1, int[][] grid2) {
  if (grid1 == null || grid1.length == 0 || grid1[0].length == 0
    || grid2 == null || grid2.length == 0 || grid2[0].length == 0) {
    return 0;
  }
  int m = grid1.length, n = grid1[0].length;
  // 把不是子岛的岛屿全部沉掉
  for (int r = 0; r < m; r++) {
    for (int c = 0; c < n; c++) {
      if (grid1[r][c] == 0 && grid2[r][c] == 1) {
        dfs(grid2, r, c);
      }
    }
  }
  // 剩余岛屿即使岛屿数量
  int result = 0;
  for (int r = 0; r < m; r++) {
    for (int c = 0; c < n; c++) {
      if (grid1[r][c] == 1 && grid2[r][c] == 1) {
        result++;
        dfs(grid2, r, c);
      }
    }
  }
  return result;
}

private void dfs(int[][] grid, int r, int c) {
  if (r < 0 || grid.length <= r
    || c < 0 || grid[r].length <= c) {
    return;
  }
  if (grid[r][c] != 1) {
    return;
  }
  grid[r][c] = 2;
  dfs(grid, r - 1, c); // 上
  dfs(grid, r + 1, c); // 下
  dfs(grid, r, c - 1); // 左
  dfs(grid, r, c + 1); // 右
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * 直接判断,把不是子岛的地方修改掉,
 * 因为有重复访问,所以效率更低,不如沉岛来的快
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2024-07-12 14:51:03
 */
public int countSubIslands(int[][] grid1, int[][] grid2) {
  if (grid1 == null || grid1.length == 0 || grid1[0].length == 0
    || grid2 == null || grid2.length == 0 || grid2[0].length == 0) {
    return 0;
  }
  int m = grid1.length, n = grid1[0].length;
  // 剩余岛屿即使岛屿数量
  int result = 0;
  for (int r = 0; r < m; r++) {
    for (int c = 0; c < n; c++) {
      if (grid1[r][c] == 1 && grid2[r][c] == 1) {
        boolean subIsland = dfs(grid1, grid2, r, c);
        if (subIsland) {
          result++;
        }
      }
    }
  }
  return result;
}

private boolean dfs(int[][] grid1, int[][] grid2, int r, int c) {
  if (r < 0 || grid1.length <= r
    || c < 0 || grid1[r].length <= c) {
    return true;
  }
  if (grid1[r][c] == 0 && grid2[r][c] == 0) {
    return true;
  }
  if (grid1[r][c] == 0 && grid2[r][c] == 1) {
    dfs(grid2, r, c);
    return false;
  }
  if (grid1[r][c] == 0 && grid2[r][c] == 2) {
    return false;
  }
  if (grid1[r][c] == 1 && grid2[r][c] == 0) {
    return true;
  }
  if (grid1[r][c] == 1 && grid2[r][c] == 2) {
    return true;
  }
  grid2[r][c] = 2;
  return dfs(grid1, grid2, r - 1, c) // 上
    && dfs(grid1, grid2, r + 1, c) // 下
    && dfs(grid1, grid2, r, c - 1) // 左
    && dfs(grid1, grid2, r, c + 1); // 右
}

private void dfs(int[][] grid, int r, int c) {
  if (r < 0 || grid.length <= r
    || c < 0 || grid[r].length <= c) {
    return;
  }
  if (grid[r][c] == 0 || grid[r][c] == 3) {
    return;
  }
  grid[r][c] = 3;
  dfs(grid, r - 1, c); // 上
  dfs(grid, r + 1, c); // 下
  dfs(grid, r, c - 1); // 左
  dfs(grid, r, c + 1); // 右
}

参考资料