-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1254-NumberOfClosedIslands.go
More file actions
134 lines (126 loc) · 4.06 KB
/
1254-NumberOfClosedIslands.go
File metadata and controls
134 lines (126 loc) · 4.06 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
// 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:
// <img src="https://assets.leetcode.com/uploads/2019/10/31/sample_3_1610.png" />
// 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:
// <img src="https://assets.leetcode.com/uploads/2019/10/31/sample_4_1610.png" />
// 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
import "fmt"
func closedIsland(grid [][]int) int {
if len(grid) <= 2 || len(grid[0]) <= 2 {
return 0
}
var dfs func (grid [][]int, i, j int, footprint [][]bool, change bool)
dfs = func (grid [][]int, i, j int, footprint [][]bool, change bool) {
if i < 0 || i >= len(grid) || j < 0 || j >= len(grid[0]) {
return
}
if footprint[i][j] == true {
return
}
if grid[i][j] == 1 {
return
}
footprint[i][j] = true
if change == true {
grid[i][j] = 1
}
dfs(grid, i-1, j, footprint, change)
dfs(grid, i+1, j, footprint, change)
dfs(grid, i, j-1, footprint, change)
dfs(grid, i, j+1, footprint, change)
}
footprint := make([][]bool, len(grid))
for i := range footprint {
footprint[i] = make([]bool, len(grid[0]))
}
for _, i := range []int{0, len(grid)-1} {
for j, v := range grid[i] {
if v == 0 {
dfs(grid, i, j, footprint, true)
}
}
}
for _, j := range []int{0, len(grid[0])-1} {
for i := range grid {
if grid[i][j] == 0 {
dfs(grid, i, j, footprint, true)
}
}
}
closed := 0
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[i]); j++ {
if grid[i][j] == 0 && footprint[i][j] == false {
dfs(grid, i, j, footprint, false)
closed++
}
}
}
return closed
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2019/10/31/sample_3_1610.png" />
// 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).
grid1 := [][]int{
{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},
}
fmt.Println(closedIsland(grid1)) // 2
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2019/10/31/sample_4_1610.png" />
// Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
// Output: 1
grid2 := [][]int{
{0,0,1,0,0},
{0,1,0,1,0},
{0,1,1,1,0},
}
fmt.Println(closedIsland(grid2)) // 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
grid3 := [][]int{
{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},
}
fmt.Println(closedIsland(grid3)) // 2
}