1254.统计封闭岛屿的数目

【LetMeFly】1254.统计封闭岛屿的数目

力扣题目链接:https://leetcode.cn/problems/number-of-closed-islands/

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

请返回 封闭岛屿 的数目。

 

示例 1:

输入: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]]
输出:2
解释:
灰色区域的岛屿是封闭岛屿,因为这座岛屿完全被水域包围(即被 1 区域包围)。

示例 2:

输入:grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
输出:1

示例 3:

输入: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]]
输出:2

 

提示:

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

方法一:BFS,先四周后中间

首先我们写一个BFS函数,从一个“0”开始广搜,将与之相连的所有“0”全部标记为“1”。

这样,我们只需要首先遍历给定地图的四周,将与四周相连的“0”送入BFS函数并标记为“1”,

接着遍历中间的“0”,将中间相邻的“0”标记为“1”的同时,记录下标记次数即为“封闭岛屿”的个数。

  • 时间复杂度$O(n\times m)$,其中$size(grid)=n\times m$
  • 空间复杂度$O(n\times m)$

AC代码

C++

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
typedef pair<int, int> pii;

const int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

class Solution {
private:
void mark(vector<vector<int>>& grid, int x, int y) {
queue<pii> q;
q.push({x, y});
grid[x][y] = 1;
while (q.size()) {
auto [x, y] = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int tx = x + directions[d][0];
int ty = y + directions[d][1];
if (tx >= 0 && tx < grid.size() && ty >= 0 && ty < grid[0].size() && grid[tx][ty] == 0) {
q.push({tx, ty});
grid[tx][ty] = 1;
}
}
}
}
public:
int closedIsland(vector<vector<int>>& grid) {
// 四周的0视为1
for (int i = 0; i < grid.size(); i++) {
if (grid[i][0] == 0) {
mark(grid, i, 0);
}
if (grid[i][grid[0].size() - 1] == 0) {
mark(grid, i, grid[0].size() - 1);
}
}
for (int j = 0; j < grid[0].size(); j++) {
if (grid[0][j] == 0) {
mark(grid, 0, j);
}
if (grid[grid.size() - 1][j] == 0) {
mark(grid, grid.size() - 1, j);
}
}
// 统计中间岛屿数量
int ans = 0;
for (int i = 1; i < grid.size() - 1; i++) {
for (int j = 1; j < grid[0].size() - 1; j++) {
if (grid[i][j] == 0) {
mark(grid, i, j);
ans++;
}
}
}
return ans;
}
};

Python

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
# from typing import List


directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]

class Solution:
def mark(self, grid: List[List[int]], x, y) -> None:
q = []
grid[x][y] = 1
q.append((x, y))
while q:
x, y = q.pop()
for dx, dy in directions:
tx = dx + x
ty = dy + y
if 0 <= tx < len(grid) and 0 <= ty < len(grid[0]) and grid[tx][ty] == 0:
grid[tx][ty] = 1
q.append((tx, ty))

def closedIsland(self, grid: List[List[int]]) -> int:
for i in range(len(grid)):
if grid[i][0] == 0:
self.mark(grid, i, 0)
if grid[i][-1] == 0:
self.mark(grid, i, len(grid[0]) - 1)
for j in range(len(grid[0])):
if grid[0][j] == 0:
self.mark(grid, 0, j)
if grid[-1][j] == 0:
self.mark(grid, len(grid) - 1, j)

ans = 0
for i in range(1, len(grid) - 1):
for j in range(1, len(grid[0]) - 1):
if grid[i][j] == 0:
self.mark(grid, i, j)
ans += 1
return ans

同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/131270866


1254.统计封闭岛屿的数目
https://blog.letmefly.xyz/2023/06/18/LeetCode 1254.统计封闭岛屿的数目/
作者
Tisfy
发布于
2023年6月18日
许可协议