-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.go
46 lines (46 loc) · 894 Bytes
/
Solution.go
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
func minimumOperations(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
vis := map[int]bool{}
match := make([]int, m*n)
for i := range match {
match[i] = -1
}
g := map[int][]int{}
for i, row := range grid {
for j, v := range row {
if (i+j)&1 == 1 && v == 1 {
x := i*n + j
if i < m-1 && grid[i+1][j] == 1 {
g[x] = append(g[x], x+n)
}
if i > 0 && grid[i-1][j] == 1 {
g[x] = append(g[x], x-n)
}
if j < n-1 && grid[i][j+1] == 1 {
g[x] = append(g[x], x+1)
}
if j > 0 && grid[i][j-1] == 1 {
g[x] = append(g[x], x-1)
}
}
}
}
var find func(int) int
find = func(i int) int {
for _, j := range g[i] {
if !vis[j] {
vis[j] = true
if match[j] == -1 || find(match[j]) == 1 {
match[j] = i
return 1
}
}
}
return 0
}
for i := range g {
ans += find(i)
vis = map[int]bool{}
}
return
}