<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 {
public:
    int uniquePathsIII(vector<vector<int>>& grid) {
        int rows = grid.size();
        int cols = grid[0].size();

        pair<int, int> start, end;
        int obstacles = 0;

        // Find the start, end, and count obstacles
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == 1) start = {i, j};
                if (grid[i][j] == 2) end = {i, j};
                if (grid[i][j] == -1) obstacles++; 
            }
        }

        // Total cells to visit, including start and end
        int traversalLimit = rows * cols - obstacles - 1;
        unordered_set<int> visited;
        int finalAnswer = 0;

        floodFlow(grid, visited, start, end, traversalLimit, finalAnswer);
        return finalAnswer;
    }

    void floodFlow(vector<vector<int>>& grid, unordered_set<int>& visited, pair<int, int> start, pair<int, int> end, int traversalLimit, int& finalAnswer) {
        int rows = grid.size();
        int cols = grid[0].size();

        // Check for out of bounds, visited cell, or obstacle
        if (start.first < 0 || start.first >= rows || start.second < 0 || start.second >= cols ||
            visited.find(start.first * cols + start.second) != visited.end() || grid[start.first][start.second] == -1) {
            return;
        }

        // Reached the end but not all squares traversed
        if (start == end && traversalLimit != 0) {
            return;
        }

        // Terminating condition: found the end with exactly the right number of steps
        if (start == end && traversalLimit == 0) {
            finalAnswer++;
            return;
        }

        // Mark the cell as visited
        int location = start.first * cols + start.second;
        visited.insert(location);

        // Recursively visit the neighboring cells (DFS)
        floodFlow(grid, visited, {start.first + 1, start.second}, end, traversalLimit - 1, finalAnswer); // go down
        floodFlow(grid, visited, {start.first, start.second + 1}, end, traversalLimit - 1, finalAnswer); // go right
        floodFlow(grid, visited, {start.first - 1, start.second}, end, traversalLimit - 1, finalAnswer); // go up
        floodFlow(grid, visited, {start.first, start.second - 1}, end, traversalLimit - 1, finalAnswer); // go left

        // Backtrack: unmark the cell as visited
        visited.erase(location);
    }
};

19971