|
| 1 | +--- |
| 2 | +id: unique-paths-iii |
| 3 | +title: Unique Paths III |
| 4 | +sidebar_label: Unique Paths III |
| 5 | +tags: [Backtracking, DFS, Grid, C++, Python, Java] |
| 6 | +description: Find the number of unique paths from the starting square to the ending square in a grid, walking over every non-obstacle square exactly once. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +You are given an `m x n` integer array `grid` where `grid[i][j]` could be: |
| 14 | + |
| 15 | +- `1` representing the starting square. There is exactly one starting square. |
| 16 | +- `2` representing the ending square. There is exactly one ending square. |
| 17 | +- `0` representing empty squares we can walk over. |
| 18 | +- `-1` representing obstacles that we cannot walk over. |
| 19 | + |
| 20 | +Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once. |
| 21 | + |
| 22 | +### Example |
| 23 | + |
| 24 | +**Example 1:** |
| 25 | +``` |
| 26 | +Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]] |
| 27 | +Output: 2 |
| 28 | +``` |
| 29 | +**Explanation:** We have the following two paths: |
| 30 | + |
| 31 | +(0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2) |
| 32 | +(0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2) |
| 33 | + |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | +``` |
| 37 | +Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]] |
| 38 | +Output: 4 |
| 39 | +``` |
| 40 | +**Explanation:** We have the following four paths: |
| 41 | + |
| 42 | +(0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3) |
| 43 | +(0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3) |
| 44 | +(0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3) |
| 45 | +(0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3) |
| 46 | + |
| 47 | + |
| 48 | +### Constraints |
| 49 | + |
| 50 | +- `m == grid.length` |
| 51 | +- `n == grid[i].length` |
| 52 | +- `1 <= m, n <= 20` |
| 53 | +- `1 <= m * n <= 20` |
| 54 | +- `-1 <= grid[i][j] <= 2` |
| 55 | +- There is exactly one starting cell and one ending cell. |
| 56 | + |
| 57 | +## Solution |
| 58 | + |
| 59 | +### Intuition |
| 60 | + |
| 61 | +To solve this problem, we can use a backtracking approach with Depth-First Search (DFS). We will start from the starting square and try to explore all possible paths to the ending square while ensuring we visit every non-obstacle square exactly once. We will backtrack whenever we hit a dead end or revisit a square. |
| 62 | + |
| 63 | +### Time Complexity and Space Complexity Analysis |
| 64 | + |
| 65 | +- **Time Complexity**: $O(4^{m \times n})$, where $m \times n$ is the total number of cells. This is because, in the worst case, we might explore all possible paths. |
| 66 | +- **Space Complexity**: $O(m \times n)$, for the recursion stack and the `visited` array. |
| 67 | + |
| 68 | +### Code |
| 69 | + |
| 70 | +#### C++ |
| 71 | + |
| 72 | +```cpp |
| 73 | +class Solution { |
| 74 | +public: |
| 75 | + int uniquePathsIII(vector<vector<int>>& grid) { |
| 76 | + int rows = grid.size(); |
| 77 | + int cols = grid[0].size(); |
| 78 | + int emptySquares = 1; // including the starting point |
| 79 | + int startX, startY; |
| 80 | + |
| 81 | + for (int i = 0; i < rows; ++i) { |
| 82 | + for (int j = 0; j < cols; ++j) { |
| 83 | + if (grid[i][j] == 0) { |
| 84 | + ++emptySquares; |
| 85 | + } else if (grid[i][j] == 1) { |
| 86 | + startX = i; |
| 87 | + startY = j; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return dfs(grid, startX, startY, emptySquares); |
| 93 | + } |
| 94 | + |
| 95 | +private: |
| 96 | + int dfs(vector<vector<int>>& grid, int x, int y, int emptySquares) { |
| 97 | + if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == -1) { |
| 98 | + return 0; |
| 99 | + } |
| 100 | + if (grid[x][y] == 2) { |
| 101 | + return emptySquares == 0 ? 1 : 0; |
| 102 | + } |
| 103 | + |
| 104 | + grid[x][y] = -1; |
| 105 | + int paths = dfs(grid, x + 1, y, emptySquares - 1) + |
| 106 | + dfs(grid, x - 1, y, emptySquares - 1) + |
| 107 | + dfs(grid, x, y + 1, emptySquares - 1) + |
| 108 | + dfs(grid, x, y - 1, emptySquares - 1); |
| 109 | + grid[x][y] = 0; |
| 110 | + |
| 111 | + return paths; |
| 112 | + } |
| 113 | +}; |
| 114 | +``` |
| 115 | +
|
| 116 | +#### Python |
| 117 | +```python |
| 118 | +class Solution: |
| 119 | + def uniquePathsIII(self, grid: List[List[int]]) -> int: |
| 120 | + rows, cols = len(grid), len(grid[0]) |
| 121 | + empty_squares = 1 # including the starting point |
| 122 | + start_x, start_y = 0, 0 |
| 123 | + |
| 124 | + for i in range(rows): |
| 125 | + for j in range(cols): |
| 126 | + if grid[i][j] == 0: |
| 127 | + empty_squares += 1 |
| 128 | + elif grid[i][j] == 1: |
| 129 | + start_x, start_y = i, j |
| 130 | + |
| 131 | + def dfs(x, y, empty_squares): |
| 132 | + if x < 0 or x >= rows or y < 0 or y >= cols or grid[x][y] == -1: |
| 133 | + return 0 |
| 134 | + if grid[x][y] == 2: |
| 135 | + return 1 if empty_squares == 0 else 0 |
| 136 | + |
| 137 | + grid[x][y] = -1 |
| 138 | + paths = (dfs(x + 1, y, empty_squares - 1) + |
| 139 | + dfs(x - 1, y, empty_squares - 1) + |
| 140 | + dfs(x, y + 1, empty_squares - 1) + |
| 141 | + dfs(x, y - 1, empty_squares - 1)) |
| 142 | + grid[x][y] = 0 |
| 143 | + |
| 144 | + return paths |
| 145 | + |
| 146 | + return dfs(start_x, start_y, empty_squares) |
| 147 | +``` |
| 148 | +#### Java |
| 149 | +```java |
| 150 | +class Solution { |
| 151 | + public int uniquePathsIII(int[][] grid) { |
| 152 | + int rows = grid.length; |
| 153 | + int cols = grid[0].length; |
| 154 | + int emptySquares = 1; // including the starting point |
| 155 | + int startX = 0, startY = 0; |
| 156 | + |
| 157 | + for (int i = 0; i < rows; i++) { |
| 158 | + for (int j = 0; j < cols; j++) { |
| 159 | + if (grid[i][j] == 0) { |
| 160 | + emptySquares++; |
| 161 | + } else if (grid[i][j] == 1) { |
| 162 | + startX = i; |
| 163 | + startY = j; |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return dfs(grid, startX, startY, emptySquares); |
| 169 | + } |
| 170 | + |
| 171 | + private int dfs(int[][] grid, int x, int y, int emptySquares) { |
| 172 | + if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] == -1) { |
| 173 | + return 0; |
| 174 | + } |
| 175 | + if (grid[x][y] == 2) { |
| 176 | + return emptySquares == 0 ? 1 : 0; |
| 177 | + } |
| 178 | + |
| 179 | + grid[x][y] = -1; |
| 180 | + int paths = dfs(grid, x + 1, y, emptySquares - 1) + |
| 181 | + dfs(grid, x - 1, y, emptySquares - 1) + |
| 182 | + dfs(grid, x, y + 1, emptySquares - 1) + |
| 183 | + dfs(grid, x, y - 1, emptySquares - 1); |
| 184 | + grid[x][y] = 0; |
| 185 | + |
| 186 | + return paths; |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
0 commit comments