Unique Paths III
HardBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.You are given a grid where:
1is the start (exactly one),2is the end (exactly one),0is an empty square you can walk on,-1is 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]] → 0Constraints
1 <= rows * cols <= 20-1 <= grid[r][c] <= 2- There is exactly one start and one end.