Given a
m
xn
binary matrix mat. In one step, you can choose one cell and flip it and all the four neighbours of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge.
Return the minimum number of steps required to convert
mat
to a zero matrix or -1 if you cannot.
Binary matrix is a matrix with all cells equal to 0 or 1 only.
Zero matrix is a matrix with all cells equal to 0.
給予一
m
xn
的邏輯矩陣。每一步中,你可以選擇一個方格翻轉它和它周圍四個鄰近且存在的格子(翻轉就是1變0、0變1)。一對鄰近的格子是指它們共用同一條邊。
回傳你將
mat
全部轉為0所需要的最小步數,如果沒辦法變成零矩陣就回傳**-1**。
邏輯矩陣是指所有的格子都只會是0或是1。
零矩陣是指全部的格子都是0。
n
和m
都不會超過3
,只需要暴力解即可。LeetCode
C++