[LeetCode] 1254. Number of Closed Islands

Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.

Return the number of closed islands.

Example 1:
Example 1
Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
Output: 2
Explanation:
Islands in gray are closed because they are completely surrounded by water (group of 1s).

Example 2:
Example 2
Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
Output: 1

Example 3:
Input: grid = [[1,1,1,1,1,1,1],
[1,0,0,0,0,0,1],
[1,0,1,1,1,0,1],
[1,0,1,0,1,0,1],
[1,0,1,1,1,0,1],
[1,0,0,0,0,0,1],
[1,1,1,1,1,1,1]]
Output: 2

Constraints:
1 <= grid.length, grid[0].length <= 100
0 <= grid[i][j] <= 1

统计封闭岛屿的数目。

二维矩阵 grid 由 0 (土地)和 1 (水)组成。岛是由最大的4个方向连通的 0 组成的群,封闭岛是一个 完全 由1包围(左、上、右、下)的岛。

请返回 封闭岛屿 的数目。

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

思路

思路还是跟岛屿类型的题一样,用 BFS 或者 DFS 做,需要遍历 input 矩阵两遍。BFS 或者 DFS 做时间空间复杂度相同。

注意这道题的规则,在 grid 边缘上的 0 是无法被完全包围的,所以我们第一轮遍历的时候先要把这些 0 变成 1。第二轮扫描的时候,剩下的 0 才是四面都被 1 包围的 0。

复杂度

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

DFS实现

Java实现

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
class Solution {
int m;
int n;

public int closedIsland(int[][] grid) {
m = grid.length;
n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// 处理边界上的0
if ((i == 0 || i == m - 1 || j == 0 || j == n - 1) && grid[i][j] == 0) {
dfs(grid, i, j);
}
}
}

int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
count++;
dfs(grid, i, j);
}
}
}
return count;
}

private void dfs(int[][] grid, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 1) {
return;
}
grid[i][j] = 1;
dfs(grid, i - 1, j);
dfs(grid, i + 1, j);
dfs(grid, i, j - 1);
dfs(grid, i, j + 1);
}
}

BFS实现

Java实现

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
class Solution {
int m;
int n;
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};

public int closedIsland(int[][] grid) {
m = grid.length;
n = grid[0].length;
// 把边界上所有的0变成1
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if ((i == 0 || i == m - 1 || j == 0 || j == n - 1) && grid[i][j] == 0) {
bfs(grid, i, j);
}
}
}

int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
count++;
bfs(grid, i, j);
}
}
}
return count;
}

private void bfs(int[][] grid, int i, int j) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] { i, j });
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int x = cur[0];
int y = cur[1];
grid[x][y] = 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 && grid[newX][newY] == 0) {
queue.offer(new int[] { newX, newY });
}
}
}
}
}

[LeetCode] 1254. Number of Closed Islands
https://shurui91.github.io/posts/1313054330.html
Author
Aaron Liu
Posted on
January 20, 2023
Licensed under