<p>Given a <code>m x n</code> binary matrix <code>mat</code>. In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing <code>1</code> to <code>0</code> and <code>0</code> to <code>1</code>). A pair of cells are called neighbors if they share one edge.</p>

<p>Return the <em>minimum number of steps</em> required to convert <code>mat</code> to a zero matrix or <code>-1</code> if you cannot.</p>

<p>A <strong>binary matrix</strong> is a matrix with all cells equal to <code>0</code> or <code>1</code> only.</p>

<p>A <strong>zero matrix</strong> is a matrix with all cells equal to <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/28/matrix.png" style="width: 409px; height: 86px;" />
<pre>
<strong>Input:</strong> mat = [[0,0],[0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> mat = [[0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Given matrix is a zero matrix. We do not need to change it.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> mat = [[1,0,0],[1,0,0]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Given matrix cannot be a zero matrix.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 3</code></li>
	<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

// bfs + bit vector.
// Since matrix is not very big, we can use a int bit vector
// to represent the matrix.
// Use queue to do bfs, use level traverse. In the beginning, 
// push the initial state matrix bit vector into queue, and insert
// it into visited set.
// While queue is not empty, if qsize = k, it means the first k elements
// in the queue are the matrix states that are reached from the original 
// matrix state with the same number of flips. Traverse the first k
// elements in the queue, for each element, check if it's zero matrix, if
// yes, return step; Otherwise, for loop the whole matrix and try to set
// every position + all valid neighbours. If for current position, the flipped
// state hasn't been visited before, push it into the queue and insert it
// into visited set.
// In the end, return -1.

// If you want to flip x, you can use XOR.
// If x is 0, x ^ 1 is 1.
// If x is 1, x ^ 1 is 0.
// so, x ^= 1 means flip x.
class Solution {
private:
    int m;
    int n;
public:
    int createBitVec(vector<vector<int>>& mat) {
        int bitVec = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                bitVec <<= 1;
                bitVec |= mat[i][j];
            }
        }
        return bitVec;
    }

    int flip(int bitVec, int i, int j) {
        vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        int pos = m * n - 1 - (i * n + j);
        bitVec ^= 1 << pos;
        for (auto d : directions) {
            int x = i + d[0];
            int y = j + d[1];
            if (x < 0 || x >= m || y < 0 || y >= n) {
                continue;
            }
            pos = m * n - 1 - (x * n + y);
            bitVec ^= 1 << pos;
        }
        return bitVec;
    }

    int minFlips(vector<vector<int>>& mat) {
        this->m = mat.size();
        this->n = mat[0].size();
        int bitVec = createBitVec(mat);
        if (bitVec == 0) {
            return 0;
        }
        queue<int> q;
        unordered_set<int> visited;
        visited.insert(bitVec);
        q.push(bitVec);
        int step = 0;
        while (!q.empty()) {
            int k = q.size();
            for (int l = 0; l < k; l++) {
                int top = q.front();
                q.pop();
                if (top == 0) {
                    return step;
                }
                for (int i = 0; i < m; i++) {
                    for (int j = 0; j < n; j++) {
                        bitVec = flip(top, i, j);
                        if (visited.count(bitVec) == 0) {
                            visited.insert(bitVec);
                            q.push(bitVec);
                        }
                    }
                }

            }
            step++;
        }
        return -1;
    }
};
19700