<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>

// 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.
// If you select one cell and flip this cell with adjacent cell, matrix 
// will be changed. But if you take this action twice(on the same cell), 
// matrix will be not changed.
// Since matrix is small in this case, we can use brute force dfs. Start
// from (0, 0) position, for each position:
// 1. Try to flip mat[i][j] and all its neighbors to get number of flip 
// needed. number of flips increase by 1, namely, dfs(mat, i, j) = 
// dfs(flip(mat), i+1, j) + 1
// 2. Try not to flip mat[i][j].number of flips stays the same, namely, 
// dfs(mat, i, j) = dfs(mat, i+1, j)
// Termination condition: If we have tried every positions, and in the 
// end it's a zero matrix, return 0; otherwise it's not a zero matrix, 
// return INT_MAX.
// Return minimal of this two results.
// Note: to dfs next position, we always use (i+1, j), then in the beginning
// of dfs, we can adjust i, j if it's out of range.
// We can potentially add a cache to record number of minial flip needed to
// reach current matrix state to improve performance. eg. unordered_map<int, int>, 
// where key is the bit vector value of the matrix.
class Solution {
private:
    int m;
    int n;
public:
    vector<vector<int>> flip(vector<vector<int>> mat, int i, int j) {
        mat[i][j] ^= 1;
        if (i-1 >= 0) {
            mat[i-1][j] ^= 1;
        }
        if (i+1 < m) {
            mat[i+1][j] ^= 1;
        }
        if (j-1 >= 0) {
            mat[i][j-1] ^= 1;
        }
        if (j+1 < n) {
            mat[i][j+1] ^= 1;
        }
        return mat;
    } 
    bool isZeroMatrix(vector<vector<int>>& mat) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (mat[i][j] != 0) {
                    return false;
                }
            }
        }
        return true;
    }

    int dfs(vector<vector<int>> mat, int i, int j) {
        if (i == m) {
            i = 0;
            j = j+1;
        }
        if (j == n) {
            if (isZeroMatrix(mat)) {
                return 0;
            } else {
                return INT_MAX;
            }
        }
        int flipMatrix = dfs(flip(mat, i, j), i+1, j);
        flipMatrix = flipMatrix == INT_MAX ? INT_MAX : flipMatrix+1;
        int noFlipMatrix = dfs(mat, i+1, j);
        return min(flipMatrix, noFlipMatrix);
    }

    int minFlips(vector<vector<int>>& mat) {
        this->m = mat.size();
        this->n = mat[0].size();
        int res = dfs(mat, 0, 0);
        return res == INT_MAX ? -1 : res;
    }
};
15100