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

template<>
struct std::hash<std::pair<int, int>> {
    std::size_t operator()(const std::pair<int, int>& pair) const 
    {
        std::hash<int> hasher{};
        std::size_t hashValue{ 0 };
        hashValue ^= hasher(pair.first) + 0x9ddfea08eb382d69ULL + (hashValue << 6) + (hashValue >> 2);
        hashValue ^= hasher(pair.second) + 0x9ddfea08eb382d69ULL + (hashValue << 6) + (hashValue >> 2);
        return hashValue;
    }
};

class Solution {
public:
    bool isValid(const int m, const int n, const int x, const int y, std::pair<int, int>& newPair, std::unordered_set<std::pair<int, int>>& visited, const std::vector<std::vector<int>>& grid)
    {
        return x >= 0 && y >= 0 && x < m && y < n && (visited.find(newPair) == visited.end()) && grid[x][y] != -1;
    }

    void backTrack(const int m, const int n, const int row, const int col, const int ending_row, const int ending_col, const int emptyCells, int cellCount, int& numOfPaths, const std::vector<std::vector<int>>& directions, std::unordered_set<std::pair<int, int>>& visited, const std::vector<std::vector<int>>& grid)
    {
        if (row == ending_row && col == ending_col)
        {
            if (cellCount == emptyCells)
            {
                ++numOfPaths;
            }
            return;
        }

        std::pair<int, int> newPair{ std::make_pair(row, col) };
        if (isValid(m, n, row, col, newPair, visited, grid))
        {
            visited.insert(newPair);
            for (const std::vector<int>& dir : directions)
            {
                int x{ row + dir[0] };
                int y{ col + dir[1] };
            
                backTrack(m, n, x, y, ending_row, ending_col, emptyCells, ++cellCount, numOfPaths, directions, visited, grid);
                --cellCount;    
            }
            visited.erase(newPair);
        }
    }

    int uniquePathsIII(std::vector<std::vector<int>>& grid)
    {
        int m{ static_cast<int>(grid.size()) };
        int n{ static_cast<int>(grid[0].size()) };
        int starting_row{ 0 };
        int starting_col{ 0 };
        int ending_row{ 0 };
        int ending_col{ 0 };
        int emptyCells{ 1 }; 

        for (int i{ 0 }; i < m; ++i)
        {
            for (int j{ 0 }; j < n; ++j)
            {
                if (grid[i][j] == 1)
                {
                    starting_row = i;
                    starting_col = j;
                }

                if (grid[i][j] == 0)
                {
                    ++emptyCells;
                }

                if (grid[i][j] == 2)
                {
                    ending_row = i;
                    ending_col = j;
                }
            }
        }

        std::vector<std::vector<int>> directions = { {1,0}, {0,1}, {-1,0}, {0,-1} };
        std::unordered_set<std::pair<int, int>> visited;
        int numOfPaths{ 0 };
        int cellCount{ 0 };
        backTrack(m, n, starting_row, starting_col, ending_row, ending_col, emptyCells, cellCount, numOfPaths, directions, visited, grid);
        return numOfPaths;
    }
};
12306