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.

Input: mat =[[0,0],[0,1]]Output: 3Explanation: One possible solution is to flip(1,0) then (0,1) and finally(1,1) as shown.
Represent the matrix as a bitmask integer, where each bit is a cell. BFS explores all possible flip sequences, and the minimum number of flips to reach the zero matrix is the answer.
classSolution {
public:int minFlips(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size();
int start =0;
for (int i =0; i < m; ++i)
for (int j =0; j < n; ++j)
if (mat[i][j]) start |=1<< (i * n + j);
queue<pair<int, int>> q;
unordered_set<int> vis;
q.push({start, 0});
vis.insert(start);
vector<pair<int,int>> dirs{{0,0},{0,1},{0,-1},{1,0},{-1,0}};
while (!q.empty()) {
auto [mask, steps] = q.front(); q.pop();
if (mask ==0) return steps;
for (int i =0; i < m; ++i) {
for (int j =0; j < n; ++j) {
int next = mask;
for (auto& d : dirs) {
int ni = i + d.first, nj = j + d.second;
if (ni >=0&& ni < m && nj >=0&& nj < n)
next ^=1<< (ni * n + nj);
}
if (!vis.count(next)) {
vis.insert(next);
q.push({next, steps+1});
}
}
}
}
return-1;
}
};
classSolution {
publicintminFlips(int[][] mat) {
int m = mat.length, n = mat[0].length;
int start = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (mat[i][j]== 1) start |= 1 << (i * n + j);
Queue<int[]> q =new LinkedList<>();
Set<Integer> vis =new HashSet<>();
q.offer(newint[]{start, 0});
vis.add(start);
int[][] dirs = {{0,0},{0,1},{0,-1},{1,0},{-1,0}};
while (!q.isEmpty()) {
int[] s = q.poll();
int mask = s[0], steps = s[1];
if (mask == 0) return steps;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int next = mask;
for (int[] d : dirs) {
int ni = i + d[0], nj = j + d[1];
if (ni >= 0 && ni < m && nj >= 0 && nj < n)
next ^= 1 << (ni * n + nj);
}
if (!vis.contains(next)) {
vis.add(next);
q.offer(newint[]{next, steps+1});
}
}
}
}
return-1;
}
}
classSolution {
funminFlips(mat: Array<IntArray>): Int {
val m = mat.size
val n = mat[0].size
var start = 0for (i in0 until m)
for (j in0 until n)
if (mat[i][j] ==1) start = start or (1 shl (i * n + j))
val vis = mutableSetOf<Int>()
val q = ArrayDeque<Pair<Int, Int>>()
q.add(start to 0)
vis.add(start)
val dirs = arrayOf(intArrayOf(0,0), intArrayOf(0,1), intArrayOf(0,-1), intArrayOf(1,0), intArrayOf(-1,0))
while (q.isNotEmpty()) {
val(mask, steps) = q.removeFirst()
if (mask ==0) return steps
for (i in0 until m) {
for (j in0 until n) {
var next = mask
for (d in dirs) {
val ni = i + d[0]; val nj = j + d[1]
if (ni in0 until m && nj in0 until n)
next = next xor (1 shl (ni * n + nj))
}
if (next !in vis) {
vis.add(next)
q.add(next to steps+1)
}
}
}
}
return -1 }
}
classSolution:
defminFlips(self, mat: list[list[int]]) -> int:
from collections import deque
m, n = len(mat), len(mat[0])
start: int =0for i in range(m):
for j in range(n):
if mat[i][j]:
start |=1<< (i * n + j)
vis = set()
q = deque([(start, 0)])
vis.add(start)
dirs = [(0,0),(0,1),(0,-1),(1,0),(-1,0)]
while q:
mask, steps = q.popleft()
if mask ==0:
return steps
for i in range(m):
for j in range(n):
next = mask
for dx, dy in dirs:
ni, nj = i+dx, j+dy
if0<=ni<m and0<=nj<n:
next ^=1<< (ni * n + nj)
if next notin vis:
vis.add(next)
q.append((next, steps+1))
return-1
use std::collections::{HashSet, VecDeque};
impl Solution {
pubfnmin_flips(mat: Vec<Vec<i32>>) -> i32 {
let m = mat.len();
let n = mat[0].len();
letmut start =0;
for i in0..m {
for j in0..n {
if mat[i][j] ==1 {
start |=1<< (i * n + j);
}
}
}
letmut vis = HashSet::new();
letmut q = VecDeque::new();
q.push_back((start, 0));
vis.insert(start);
let dirs = [(0,0),(0,1),(0,-1),(1,0),(-1,0)];
whilelet Some((mask, steps)) = q.pop_front() {
if mask ==0 { return steps; }
for i in0..m {
for j in0..n {
letmut next = mask;
for&(dx, dy) in&dirs {
let ni = i asi32+ dx;
let nj = j asi32+ dy;
if ni >=0&& ni < m asi32&& nj >=0&& nj < n asi32 {
next ^=1<< (ni asusize* n + nj asusize);
}
}
if!vis.contains(&next) {
vis.insert(next);
q.push_back((next, steps+1));
}
}
}
}
-1 }
}