<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:
    
    void solve(vector<vector<int>>& grid, int i, int j, pair<int,int>&end,vector<vector<bool>>&visited,int& ans){
        if(make_pair(i,j) == end){
            visited[i][j] = true;
            for(auto l : visited){
                for(auto m : l){
                    // cout << m <<" ";
                    if(m == false) return;
                }
                // cout << endl;
            }
            // cout << endl;
            ans += 1;
            return;
        }
        
        // up
        if(i-1 >= 0 && j >= 0 && j < grid[0].size() && visited[i-1][j] == false){
            // cout << "h1" <<" ";
             visited[i-1][j] = true;
             solve(grid,i-1,j,end,visited,ans);
             visited[i-1][j] = false;
        }

        if(i+1 < grid.size() && j >= 0 && j < grid[0].size() && visited[i+1][j] == false){
            //  cout << "h2" <<" ";
            visited[i+1][j] = true;
            solve(grid,i+1,j,end,visited,ans);
            visited[i+1][j] = false;
        }

        if(j-1 >= 0 && i >= 0 && i < grid.size() && visited[i][j-1] == false){
            //  cout << "h3" <<" ";
            visited[i][j-1] = true;
            solve(grid,i,j-1,end,visited,ans);
            visited[i][j-1] = false;
        }

        if(j+1 < grid[0].size() && i >= 0 && i < grid.size() && visited[i][j+1] == false){
            //  cout << "h4" <<" ";
            visited[i][j+1] = true;
            solve(grid,i,j+1,end,visited,ans);
            visited[i][j+1] = false;
        }
        
    }

    int uniquePathsIII(vector<vector<int>>& grid) {
        int row = grid.size();
        int col = grid[0].size();

        vector<vector<bool>>visited(row,vector<bool>(col,false));
        pair<int,int> start;
        pair<int,int>end;
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(grid[i][j] == 1){
                    start = make_pair(i,j);
                }
                else if(grid[i][j] == 2){
                    end = make_pair(i,j);
                }
                else if(grid[i][j] == -1) visited[i][j] = true;
            }
        }
        
        int ans = 0;
        visited[start.first][start.second] = true;
        // cout << start.first<<" " << start.second << endl;
        solve(grid,start.first,start.second,end,visited,ans);
        return ans;

    }
};
15591