-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1284-MinimumNumberOfFlipsToConvertBinaryMatrixToZeroMatrix.go
More file actions
186 lines (173 loc) · 5.47 KB
/
1284-MinimumNumberOfFlipsToConvertBinaryMatrixToZeroMatrix.go
File metadata and controls
186 lines (173 loc) · 5.47 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
// 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
// Given a m x n binary matrix mat.
// In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing 1 to 0 and 0 to 1).
// A pair of cells are called neighbors if they share one edge.
// Return the minimum number of steps required to convert mat to a zero matrix or -1 if you cannot.
// A binary matrix is a matrix with all cells equal to 0 or 1 only.
// A zero matrix is a matrix with all cells equal to 0.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2019/11/28/matrix.png" />
// Input: mat = [[0,0],[0,1]]
// Output: 3
// Explanation: One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.
// Example 2:
// Input: mat = [[0]]
// Output: 0
// Explanation: Given matrix is a zero matrix. We do not need to change it.
// Example 3:
// Input: mat = [[1,0,0],[1,0,0]]
// Output: -1
// Explanation: Given matrix cannot be a zero matrix.
// Constraints:
// m == mat.length
// n == mat[i].length
// 1 <= m, n <= 3
// mat[i][j] is either 0 or 1.
import "fmt"
import "strconv"
import "strings"
func minFlips(mat [][]int) int {
//brute force BFS for every status
//each status has m*n options to flip
m, n := len(mat),len(mat[0])
queue, set:= make([]string, 0, 0), make(map[string]bool)
target, temp := "", ""
flip := func(b byte)byte{
if b == '1' { return '0' }
return '1'
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
temp = temp + strconv.Itoa(mat[i][j])
target = target + "0"
}
}
queue = append(queue, string(temp))
set[string(temp)] = true
step := 0
for len(queue) > 0 {
size := len(queue)
for t := 0; t < size; t++ {
cur := queue[t]
if cur==target{
return step
}
for i := 0; i < len(cur); i++ {
next:=[]byte(cur)
next[i] = flip(next[i])
x := i / n
y := i % n
if x>0{
next[i-n] = flip(next[i-n])
}
if x<m-1{
next[i+n] = flip(next[i+n])
}
if y>0 {
next[i-1] = flip(next[i-1])
}
if y<n-1{
next[i+1] = flip(next[i+1])
}
nextstr := string(next)
if !set[nextstr]{
set[nextstr] = true
queue = append(queue, nextstr)
}
}
}
step++
queue = queue[size:]
}
return -1
}
func minFlips1(mat [][]int) int {
m, n := len(mat), len(mat[0])
target := strings.Repeat("0", m * n)
neighbours := func(pos int) []int {
x, y, res := pos / n, pos % n, []int{}
ds := [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
for _, d := range ds {
nx, ny := x + d[0], y + d[1]
if 0 <= nx && nx < m && 0 <= ny && ny < n {
res = append(res, nx*n+ny)
}
}
return res
}
getNextStatus := func(status string) []string {
bs, res := []byte(status), []string{}
for i := 0; i < m*n; i++ {
indices2Flip := append(neighbours(i), i)
for _, idx := range indices2Flip {
if bs[idx] == '0' {
bs[idx] = '1'
} else {
bs[idx] = '0'
}
}
res = append(res, string(bs))
for _, idx := range indices2Flip { // flip back
if bs[idx] == '0' {
bs[idx] = '1'
} else {
bs[idx] = '0'
}
}
}
return res
}
sb := make([]byte, 0, m*n)
for _, rows := range mat {
for _, c := range rows {
sb = append(sb, byte('0'+c))
}
}
start := string(sb)
if start == target {
return 0
}
seen := map[string]bool{}
seen[start] = true
type pair struct {
status string
step int
}
queue := []*pair{{start, 0}}
for len(queue) > 0 {
cur := queue[0] // pop
queue = queue[1:]
for _, nextStatus := range getNextStatus(cur.status) {
if !seen[nextStatus] {
if nextStatus == target {
return cur.step + 1
}
seen[nextStatus] = true
queue = append(queue, &pair{nextStatus, cur.step+1})
}
}
}
return -1
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2019/11/28/matrix.png" />
// Input: mat = [[0,0],[0,1]]
// Output: 3
// Explanation: One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.
fmt.Println(minFlips([][]int{{0,0},{0,1}})) // 3
// Example 2:
// Input: mat = [[0]]
// Output: 0
// Explanation: Given matrix is a zero matrix. We do not need to change it.
fmt.Println(minFlips([][]int{{0}})) // 0
// Example 3:
// Input: mat = [[1,0,0],[1,0,0]]
// Output: -1
// Explanation: Given matrix cannot be a zero matrix.
fmt.Println(minFlips([][]int{{1,0,0},{1,0,0}})) // -1
fmt.Println(minFlips1([][]int{{0,0},{0,1}})) // 3
fmt.Println(minFlips1([][]int{{0}})) // 0
fmt.Println(minFlips1([][]int{{1,0,0},{1,0,0}})) // -1
}