Skip to content

Unique Paths III

HardBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

You are given a grid where:

  • 1 is the start (exactly one),
  • 2 is the end (exactly one),
  • 0 is an empty square you can walk on,
  • -1 is a wall.

Moving up, down, left or right, count the walks from start to end that visit every non-wall square exactly once.

[[1, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 2, -1]]   →  2
 
[[1, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 2]]    →  4
 
[[0, 1],
 [2, 0]]          →  0

Constraints

  • 1 <= rows * cols <= 20
  • -1 <= grid[r][c] <= 2
  • There is exactly one start and one end.