|
| 1 | +# 980. Unique Paths III |
| 2 | + |
| 3 | +- Difficulty: Hard. |
| 4 | +- Related Topics: Array, Backtracking, Bit Manipulation, Matrix. |
| 5 | +- Similar Questions: Sudoku Solver, Unique Paths II, Word Search II. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given an `m x n` integer array `grid` where `grid[i][j]` could be: |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- `1` representing the starting square. There is exactly one starting square. |
| 14 | + |
| 15 | +- `2` representing the ending square. There is exactly one ending square. |
| 16 | + |
| 17 | +- `0` representing empty squares we can walk over. |
| 18 | + |
| 19 | +- `-1` representing obstacles that we cannot walk over. |
| 20 | + |
| 21 | + |
| 22 | +Return **the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once**. |
| 23 | + |
| 24 | + |
| 25 | +Example 1: |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +``` |
| 30 | +Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]] |
| 31 | +Output: 2 |
| 32 | +Explanation: We have the following two paths: |
| 33 | +1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2) |
| 34 | +2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2) |
| 35 | +``` |
| 36 | + |
| 37 | +Example 2: |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +``` |
| 42 | +Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]] |
| 43 | +Output: 4 |
| 44 | +Explanation: We have the following four paths: |
| 45 | +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) |
| 46 | +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) |
| 47 | +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) |
| 48 | +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) |
| 49 | +``` |
| 50 | + |
| 51 | +Example 3: |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +``` |
| 56 | +Input: grid = [[0,1],[2,0]] |
| 57 | +Output: 0 |
| 58 | +Explanation: There is no path that walks over every empty square exactly once. |
| 59 | +Note that the starting and ending square can be anywhere in the grid. |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +**Constraints:** |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +- `m == grid.length` |
| 68 | + |
| 69 | +- `n == grid[i].length` |
| 70 | + |
| 71 | +- `1 <= m, n <= 20` |
| 72 | + |
| 73 | +- `1 <= m * n <= 20` |
| 74 | + |
| 75 | +- `-1 <= grid[i][j] <= 2` |
| 76 | + |
| 77 | +- There is exactly one starting cell and one ending cell. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +## Solution |
| 82 | + |
| 83 | +```javascript |
| 84 | +/** |
| 85 | + * @param {number[][]} grid |
| 86 | + * @return {number} |
| 87 | + */ |
| 88 | +var uniquePathsIII = function(grid) { |
| 89 | + var start; |
| 90 | + var m = grid.length; |
| 91 | + var n = grid[0].length; |
| 92 | + var emptyNum = 0; |
| 93 | + for (var i = 0; i < m; i++) { |
| 94 | + for (var j = 0; j < n; j++) { |
| 95 | + if (grid[i][j] === 1) start = [i, j]; |
| 96 | + else if (grid[i][j] === 0) emptyNum++; |
| 97 | + } |
| 98 | + } |
| 99 | + return getPathNum(start[0], start[1], grid, 0, emptyNum); |
| 100 | +}; |
| 101 | + |
| 102 | +var getPathNum = function(i, j, grid, visitedNum, emptyNum) { |
| 103 | + var res = 0; |
| 104 | + var directions = [ |
| 105 | + [1, 0], // up |
| 106 | + [-1, 0], // down |
| 107 | + [0, -1], // left |
| 108 | + [0, 1], // right |
| 109 | + ]; |
| 110 | + for (var k = 0; k < 4; k++) { |
| 111 | + var [diffX, diffY] = directions[k]; |
| 112 | + if (grid[i + diffX] && grid[i + diffX][j + diffY] === 0) { |
| 113 | + grid[i + diffX][j + diffY] = -1; |
| 114 | + res += getPathNum(i + diffX, j + diffY, grid, visitedNum + 1, emptyNum); |
| 115 | + grid[i + diffX][j + diffY] = 0; |
| 116 | + } else if (grid[i + diffX] && grid[i + diffX][j + diffY] === 2) { |
| 117 | + res += (visitedNum === emptyNum ? 1 : 0); |
| 118 | + } |
| 119 | + } |
| 120 | + return res; |
| 121 | +}; |
| 122 | +``` |
| 123 | + |
| 124 | +**Explain:** |
| 125 | + |
| 126 | +nope. |
| 127 | + |
| 128 | +**Complexity:** |
| 129 | + |
| 130 | +* Time complexity : O(n!). |
| 131 | +* Space complexity : O(1). |
0 commit comments