<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:
    // Move to next point
    vector<vector<int>> move_next = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    int uniquePathsIII(vector<vector<int>>& grid) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);

        int m = grid.size(), n = grid[0].size();

        vector<int> start_pos(2), end_pos(2); // Define start and end point
        int step = 0, ans = 0, count = 0; // Define the number of steps and final ans

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(grid[i][j] == 0) step++;
                else if(grid[i][j] == 1) {
                    start_pos[0] = i;
                    start_pos[1] = j;
                }
                else if(grid[i][j] == 2){
                    end_pos[0] = i;
                    end_pos[1] = j;
                }
            }
        }
        step++;

        vector<int> curr_pos(2);
        curr_pos = start_pos;
        uniquePaths_backtracking(grid, m, n, step, start_pos, curr_pos, end_pos, count, ans);

        return ans;
    }

    bool canGo(vector<int>& pos, int &m, int &n, vector<vector<int>>& grid){
        if(pos[0] > -1 && pos[0] < m && pos[1] > -1 && pos[1] < n){
            if(grid[pos[0]][pos[1]] == -1) return false;
            else return true;
        }
        else return false;
    }

    void uniquePaths_backtracking(vector<vector<int>>& grid, int &m, int &n, int &step, vector<int>& start_pos, vector<int>& curr_pos, vector<int>& end_pos, int &count, int &ans){
        if(count == step && curr_pos == end_pos){
            ans++;
            return;
        }
        else if(curr_pos == end_pos) return;

        for(int i = 0; i < 4; i++){
            grid[curr_pos[0]][curr_pos[1]] = -1;
            curr_pos[0] = curr_pos[0] + move_next[i][0];
            curr_pos[1] = curr_pos[1] + move_next[i][1];
            if(canGo(curr_pos, m, n, grid)){
                count++;
                uniquePaths_backtracking(grid, m, n, step, start_pos, curr_pos, end_pos, count, ans);
                if(curr_pos == start_pos) grid[curr_pos[0]][curr_pos[1]] = 1;
                else grid[curr_pos[0]][curr_pos[1]] = 0;
                curr_pos[0] = curr_pos[0] - move_next[i][0];
                curr_pos[1] = curr_pos[1] - move_next[i][1];
                count--;
            }
            else{
                curr_pos[0] = curr_pos[0] - move_next[i][0];
                curr_pos[1] = curr_pos[1] - move_next[i][1];
            }
        }
        return;
    }
};
9021