Unique Paths III

Hard

You are given an m x n integer array grid. Each square holds one of these values:

  • 1 is the start. There is exactly one.
  • 2 is the end. There is exactly one.
  • 0 is an empty square you can walk over.
  • -1 is an obstacle you can't walk over.

Each step moves to the square directly up, down, left or right. Return the number of walks from the start to the end that step on every square that isn't an obstacle exactly once, the start and end included.

Examples

Example 1

Input:  grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Example 1: the two walksSESEwalk 1walk 2S start, E end, X obstacle

Example 2

Input:  grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4

With the obstacle gone, the end moves to the corner and there are 12 squares to cover. Four different walks cover all of them.

Example 3

Input:  grid = [[0,1],[2,0]]
Output: 0

Each 0 is only next to the start and the end. After stepping on one of them, the only way on is the end, so the other 0 is never reached.

Example 4

Input:  grid = [[0,1,2]]
Output: 0
Example 4: grid = [[0,1,2]]SE0Left: stuck at the 0, the onlyway on is back through S.Right: reaches E, the 0 is missed.

The 0 is on the far side of the start. Going there first leaves no way back, and going to the end first skips it.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 20
  • 1 <= m * n <= 20
  • -1 <= grid[i][j] <= 2
  • There is exactly one start and exactly one end.
TypeScript
Loading editor…
Test cases37
Case 1
grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Case 2
grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Case 3
grid = [[0,1],[2,0]]
Case 4
grid = [[1,2]]
Case 5
grid = [[0,1,2]]
Case 6
grid = [[1,0,2]]
Case 7
grid = [[1,-1],[0,2]]
Case 8
grid = [[2,1]]
Case 9
grid = [[1],[2]]
Case 10
grid = [[1,-1,2]]
Case 11
grid = [[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2]]
Case 12
grid = [[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[2]]
Case 13
grid = [[0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0]]
Case 14
grid = [[1,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,2]]
Case 15
grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,2]]
Case 16
grid = [[1,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,2]]
Case 17
grid = [[1,0,0,0,0,0,0,0,0,2],[0,0,0,0,0,0,0,0,0,0]]
Case 18
grid = [[0,0,0,0,0],[0,0,1,0,0],[0,0,0,0,0],[0,0,0,0,2]]
Case 19
grid = [[1,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,2,0]]
Case 20
grid = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,1,2,0,0]]
Case 21
grid = [[1,0,0],[0,-1,0],[0,0,2]]
Case 22
grid = [[1,0,0],[0,-1,0],[2,0,0]]
Case 23
grid = [[1,0,-1],[0,-1,0],[0,0,2]]
Case 24
grid = [[1,-1,-1,-1],[-1,-1,-1,-1],[-1,-1,-1,2]]
Case 25
grid = [[1,0,0,0,0],[0,0,0,0,0],[0,2,0,0,0],[0,0,0,0,0]]
Case 26
grid = [[1,0,0,0,2],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
Case 27
grid = [[0,0,0,-1,0],[0,0,-1,0,0],[0,0,0,0,0],[0,1,0,0,2]]
Case 28
grid = [[0,0,1,0],[0,0,0,0],[0,0,0,0],[0,0,0,-1],[2,0,0,0]]
Case 29
grid = [[0,0,-1,0,0],[0,0,1,0,0],[0,-1,0,-1,0],[0,0,0,2,0]]
Case 30
grid = [[0,0,2,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,1]]
Case 31
grid = [[0,0,0],[0,2,0],[0,0,0],[1,0,0],[-1,0,0],[-1,0,0]]
Case 32
grid = [[0,0,0,1,-1,0,0,0,0],[0,0,0,0,0,2,0,0,0]]
Case 33
grid = [[1,0,0,0,0],[0,0,0,0,0],[0,0,2,0,0]]
Case 34
grid = [[0,2,0,0],[0,0,0,0],[0,0,0,0],[0,1,0,0]]
Case 35
grid = [[-1,0,0,0],[0,0,0,0],[1,2,0,0],[-1,0,0,-1]]
Case 36
grid = [[0,0,1],[0,0,0],[0,2,0]]
Case 37
grid = [[0,0,1],[2,0,0]]