Shortest Path in a Grid with Obstacles Elimination - Problem

You're navigating through a dangerous maze represented by an m ร— n grid where each cell is either 0 (safe passage) or 1 (deadly obstacle). Your mission: find the shortest path from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1).

Here's the twist: you have a special power that allows you to eliminate up to k obstacles along your journey! You can move in four directions (up, down, left, right), but only through empty cells or obstacles you choose to eliminate.

Goal: Return the minimum number of steps needed to reach the destination. If it's impossible even with your obstacle-elimination power, return -1.

Example: In a 3ร—3 grid with obstacles, you might need to eliminate 1 obstacle to create the shortest path of 6 steps instead of taking a longer detour.

Input & Output

example_1.py โ€” Basic Grid with Obstacles
$ Input: grid = [[0,0,0],[1,1,0],[0,0,0]], k = 1
โ€บ Output: 6
๐Ÿ’ก Note: The shortest path without eliminating obstacles would be (0,0)โ†’(1,0)โ†’(2,0)โ†’(2,1)โ†’(2,2) = 4 steps, but there's an obstacle at (1,0). With k=1, we can eliminate one obstacle: optimal path is (0,0)โ†’(0,1)โ†’(0,2)โ†’(1,2)โ†’(2,2) = 4 steps, or eliminate obstacle at (1,1) for a different 6-step path.
example_2.py โ€” Straight Line Path
$ Input: grid = [[0,1,1],[1,1,1],[1,0,0]], k = 1
โ€บ Output: -1
๐Ÿ’ก Note: Even with 1 elimination, we cannot reach the destination. We need at least 2 eliminations to create any viable path from start to end.
example_3.py โ€” Optimal Elimination Usage
$ Input: grid = [[0,0,0,0,0,0,0,0,0,0],[0,1,1,1,1,1,1,1,1,0],[0,1,0,0,0,0,0,0,0,0],[0,1,0,1,1,1,1,1,1,1],[0,1,0,0,0,0,0,0,0,0],[0,1,1,1,1,1,1,1,1,0],[0,0,0,0,0,0,0,0,0,0]], k = 1
โ€บ Output: 12
๐Ÿ’ก Note: The grid has a maze-like structure. With k=1, we can eliminate one strategic obstacle to create the shortest path of 12 steps, rather than taking a much longer route around the obstacles.

Visualization

Tap to expand
๐Ÿค– Robot Navigator with Blast Shields๐Ÿค–START๐Ÿ’ฃMINESAFE๐Ÿ’ฅTARGETBFS Strategy VisualizationLevel 0:Robot at (0,0) with 2 shields ๐Ÿ›ก๏ธ๐Ÿ›ก๏ธLevel 1:Try right (mine - use shield) or down (safe)Level 2:Multiple states: (1,0,2), (0,1,1) - track all paths!Result:First to reach bomb = shortest path โœ…๐Ÿ”‘ Key: BFS explores by distance, guaranteeing optimal solutionUse ShieldSafe Routeโšก Time Complexity: O(m ร— n ร— k)Each cell visited once per shield countSpace: O(m ร— n ร— k) for state tracking
Understanding the Visualization
1
Deploy Robot
Robot starts at top-left with k blast shields available
2
Navigate Smartly
Use BFS to explore all paths simultaneously, tracking position and remaining shields
3
Make Decisions
At each mine, decide: use a shield or find alternate route
4
Reach Target
First arrival at bomb location is guaranteed to be the fastest route
Key Takeaway
๐ŸŽฏ Key Insight: BFS with 3D state (row, col, shields_left) ensures we find the shortest path while optimally managing our limited resources. The first time we reach the destination is guaranteed to be optimal!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m*n*k)

We might visit each cell with each possible elimination count, and BFS visits each state at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(m*n*k)

Queue and visited set can store up to m*n*k states

n
2n
โšก Linearithmic Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 โ‰ค m, n โ‰ค 40
  • 1 โ‰ค k โ‰ค m ร— n
  • grid[i][j] is either 0 or 1
  • grid[0][0] == grid[m-1][n-1] == 0
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
52.1K Views
Medium-High 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