<p>You are given an <code>m x n</code> integer array <code>grid</code> where <code>grid[i][j]</code> could be:</p>

<ul>
	<li><code>1</code> representing the starting square. There is exactly one starting square.</li>
	<li><code>2</code> representing the ending square. There is exactly one ending square.</li>
	<li><code>0</code> representing empty squares we can walk over.</li>
	<li><code>-1</code> representing obstacles that we cannot walk over.</li>
</ul>

<p>Return <em>the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique1.jpg" style="width: 324px; height: 245px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> We have the following two paths: 
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique2.jpg" style="width: 324px; height: 245px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> We have the following four paths: 
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique3-.jpg" style="width: 164px; height: 165px;" />
<pre>
<strong>Input:</strong> grid = [[0,1],[2,0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 20</code></li>
	<li><code>1 &lt;= m * n &lt;= 20</code></li>
	<li><code>-1 &lt;= grid[i][j] &lt;= 2</code></li>
	<li>There is exactly one starting cell and one ending cell.</li>
</ul>

class Solution {
    struct Vec2 {
        int x;
        int y;
        Vec2(int x, int y) : x(x), y(y) {}
    };

    int whiteSquares;  // Only count cells with 0 (walkable squares)

    vector<Vec2> getDirections(Vec2 pos, vector<vector<int>>& grid) {
        vector<Vec2> ans;
        int rows = grid.size();
        if (rows == 0) return ans;
        int cols = grid[0].size();

        // Go up.
        if (pos.y > 0) {
            ans.push_back({pos.x, pos.y - 1});
        }

        // Go left.
        if (pos.x > 0) {
            ans.push_back({pos.x - 1, pos.y});
        }

        // Go right.
        if (pos.x < cols - 1) {
            ans.push_back({pos.x + 1, pos.y});
        }

        // Go down.
        if (pos.y < rows - 1) {
            ans.push_back({pos.x, pos.y + 1});
        }

        return ans;
    }

    void backtrack(vector<vector<int>>& grid, int& ans, Vec2 pos, int walked) {
        int point = grid[pos.y][pos.x];
        
        if (point == -1) {
            return;
        }

        if (point == 2 && walked == this->whiteSquares) {
            ans++;
            return;
        }

        if (point == 0) {
            walked++;  // Count the white square we just walked on.
        }

        // Mark this cell as visited by setting it to -1.
        grid[pos.y][pos.x] = -1;

        // Recurse for all four possible directions.
        vector<Vec2> nextPos = getDirections(pos, grid);
        for (Vec2 p : nextPos) {
            backtrack(grid, ans, p, walked);
        }

        // Unmark the cell to backtrack.
        grid[pos.y][pos.x] = point;
    }

public:
    int uniquePathsIII(vector<vector<int>>& grid) {
        int ans = 0;
        Vec2 start(0, 0);
        whiteSquares = 0;

        // Identify the start position and count the white squares (cells with 0).
        for (int i = 0; i < grid.size(); i++) {
            for (int j = 0; j < grid[0].size(); j++) {
                if (grid[i][j] == 1) {
                    start = Vec2(j, i);  // Starting position
                } else if (grid[i][j] == 0) {
                    whiteSquares++;  // Count white squares
                }
            }
        }

        // Begin backtracking from the start position.
        backtrack(grid, ans, start, 0);
        return ans;
    }
};
16686