Unique Paths III - Problem

🎯 Grid Path Explorer Challenge

Imagine you're a robot explorer navigating a mysterious grid-based terrain. Your mission is to find all possible paths from your starting position to the target destination with one crucial constraint: you must visit every accessible square exactly once!

You're given an m Γ— n integer grid where each cell represents:

  • 1 - Your starting square (exactly one exists)
  • 2 - The ending square (exactly one exists)
  • 0 - Empty squares you can walk over
  • -1 - Obstacles that block your path

Goal: Return the number of unique 4-directional walks from start to end that visit every non-obstacle square exactly once.

Example: In a 3Γ—3 grid with start at (0,0), end at (2,2), and one obstacle, you might find 4 different valid paths that cover all walkable squares.

Input & Output

example_1.py β€” Basic Grid
$ Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
β€Ί Output: 2
πŸ’‘ Note: There are exactly 2 paths that visit every walkable square exactly once and end at the target. Both paths start from (0,0), visit all zeros, and end at (2,2).
example_2.py β€” Simple Path
$ Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
β€Ί Output: 4
πŸ’‘ Note: With no obstacles, there are 4 different ways to traverse all squares exactly once from start to end, considering different path combinations.
example_3.py β€” No Valid Path
$ Input: grid = [[0,1],[2,0]]
β€Ί Output: 0
πŸ’‘ Note: There's no valid path because we cannot visit all walkable squares exactly once due to the grid layout constraints.

Visualization

Tap to expand
1000-12Path Finding Process:🟒 Start (1) β†’ πŸ”΄ End (2)⬜ Walkable (0) β†’ ⬛ Obstacle (-1)1. Count walkable squares: 42. Try paths: Start β†’ 0 β†’ 0 β†’ End3. Backtrack when stuck4. Find all valid complete paths
Understanding the Visualization
1
Scout the Territory
Count all walkable squares and locate start/end positions
2
Begin Exploration
Start DFS from the beginning position, trying each direction
3
Mark Progress
Temporarily mark visited squares to avoid revisiting in current path
4
Check Victory
When reaching end, verify all walkable squares were visited
5
Backtrack & Continue
Unmark squares and try alternative routes to find all solutions
Key Takeaway
🎯 Key Insight: Backtracking with state management allows us to systematically explore all possible paths while ensuring we visit every walkable square exactly once

Time & Space Complexity

Time Complexity
⏱️
O(4^(mΓ—n))

In worst case, we explore 4 directions for each of the mΓ—n cells, leading to exponential time complexity

n
2n
βœ“ Linear Growth
Space Complexity
O(mΓ—n)

Space for recursion stack and visited array, proportional to grid size

n
2n
⚑ Linearithmic Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≀ m, n ≀ 20
  • 1 ≀ m Γ— n ≀ 20
  • grid[i][j] is -1, 0, 1, or 2
  • There is exactly one starting square and one ending square
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
89.2K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen