[Swift]LeetCode1254. 统计封闭岛屿的数目 | Number of Closed Islands

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(let_us_code)
➤博主域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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:

[Swift]LeetCode1254. 统计封闭岛屿的数目 | Number of Closed Islands

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:

[Swift]LeetCode1254. 统计封闭岛屿的数目 | Number of Closed Islands

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 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座「岛屿」。

如果一座岛屿 完全 由水域包围,即陆地边缘上下左右所有相邻区域都是水域,那么我们将其称为 「封闭岛屿」。

请返回封闭岛屿的数目。

示例 1:

[Swift]LeetCode1254. 统计封闭岛屿的数目 | Number of Closed Islands

输入: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:

[Swift]LeetCode1254. 统计封闭岛屿的数目 | Number of Closed Islands

输入: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

Runtime: 92 ms
Memory Usage: 21.1 MB
 1 class Solution {
 2     func closedIsland(_ grid: [[Int]]) -> Int {
 3         var grid = grid
 4         for i in 0..<grid.count
 5         {
 6             for j in 0..<grid[i].count
 7             {
 8                 if i == 0 || j == 0 || i == grid.count - 1 || j == grid[i].count - 1
 9                 {
10                     fill(&grid, i, j)
11                 }
12             }
13         }
14         var res:Int = 0
15         for i in 0..<grid.count
16         {
17             for j in 0..<grid[i].count
18             {
19                 res += fill(&grid, i, j) > 0 ? 1 : 0
20             }
21         }
22         return res
23     }
24     
25     func fill(_ g: inout [[Int]],_ i:Int,_ j:Int) -> Int
26     {
27         if i < 0 || j < 0 || i >= g.count || j >= g[i].count || g[i][j] != 0
28         {
29             return 0
30         }
31         g[i][j] = 1
32         return g[i][j] + fill(&g, i + 1, j) + fill(&g, i, j + 1) + fill(&g, i - 1, j) + fill(&g, i, j - 1)
33     }
34 }