π― 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
Visualization
Time & Space Complexity
In worst case, we explore 4 directions for each of the mΓn cells, leading to exponential time complexity
Space for recursion stack and visited array, proportional to grid size
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