← Back to Visualizer Gallery
Traversal · State-Space BFS LeetCode 1293

Shortest Path with Obstacles Elimination / tuple state (r, c, k)

Walking a grid with at most $k$ obstacle eliminations: the state space is the 3D tuple $(r, c, k_{\text{rem}})$. A 2D visited array causes false deadends — use dominance pruning with maxK[r][c].

Execution Stage

idle
INVARIANT Dominance Pruning Invariant: A state is $(r, c, k)$. We maintain bestK[r][c] recording the maximum remaining elimination quota ever seen at $(r, c)$. We only prune if $k \le \text{bestK}[r][c]$.
Steps Taken 0
Active Quota k 0
Queue Size 0
Press Run to trace 3D state BFS.
0 / 0
Speed

Java · Reference Algorithm

INTERVIEW SCRIPT

How to Present Shortest Path with Obstacles to an Interviewer

1. Why 2D Visited Fails

"A 2D visited[r][c] records physical cell reachability, but ignores the remaining elimination budget $k$. A path arriving earlier with $k=0$ would incorrectly block a later path with $k=2$ that needs that quota to penetrate the exit wall!"

2. State Definition & Dominance Pruning

"The state is $(r, c, k)$. Rather than a 3D boolean array, we maintain int[][] maxK initialized to -1. A state is pruned if and only if its remaining $k \le \text{maxK}[r][c]$."

3. The Manhattan Shortcut

"If $k \ge (m - 1) + (n - 1) - 1$, we have enough eliminations to blast straight through any obstacle wall along the shortest path! We can return $(m - 1) + (n - 1)$ in $O(1)$ immediately."

4. Complexity

"With state space $(r, c, k)$, there are $O(m \cdot n \cdot k)$ distinct states, each branching in 4 directions. Time is $O(m \cdot n \cdot k)$ and space is $O(m \cdot n \cdot k)$."

Shortest Path with Obstacles (LC 1293) — State space BFS with dominance pruning · Time $O(mnk)$, Space $O(mnk)$.