Skip to content
Decoder

Unique Paths III

Hard

Problem

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.

Example 1

[[1, 0, 0, 0],

Example 2

 [0, 0, 0, 0],

Example 3

Input
[0, 0, 2, -1]]
Output
2

Example 4

[[1, 0, 0, 0],

Example 5

 [0, 0, 0, 0],

Example 6

Input
[0, 0, 0, 2]]
Output
4

Example 7

[[0, 1],

Example 8

Input
[2, 0]]
Output
0

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.