[Swift] LeetCode1254 number of statistical enclosed islands |. Number of Closed Islands

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: to dare (WeiGanTechnologies)
➤ personal domain name: https://www.zengqiang.org
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: https://www.cnblogs.com/strengthen/p/11831493.html
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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:

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:

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

There is a two-dimensional matrix  grid , each location either on land (to mark  0 ) is either water (for the sign  1 ).

We proceed from one land mass, each can up under the direction of the adjacent area of ​​about four to go, and went to all land areas, which we will call an "island."

If an island completely surrounded by water, that is, all the adjacent land areas around the edge of the waters are up and down, then we will call it "closed the island."

Please return the number of closed islands.

 

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: 
island island gray area is closed, because the island is completely surrounded by water (i.e., an area enclosed).

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

prompt:

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

Guess you like

Origin www.cnblogs.com/strengthen/p/11831493.html