2Brute2Best
🔥0

Shortest Path in a Grid with Obstacles Elimination

Hard·LeetCode #1293 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given an m × n grid. A 0 is an empty cell and a 1 is an obstacle. You start at the top-left cell and want to reach the bottom-right cell.

Each move goes up, down, left or right into a neighbouring cell. You may remove up to k obstacles on the way: stepping onto an obstacle and removing it is allowed while you still have removals left. The start and end cells are empty.

Return the fewest moves needed to reach the bottom-right cell. Return -1 if no walk does it with at most k removals.

Input

grid, an m × n grid of 0 and 1, and k, the number of obstacles you may remove.

Output

The fewest moves from the top-left to the bottom-right cell, or -1.

Example 1
Input: grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1
Output: 6

Going round the obstacles without removing any takes 10 moves. Removing the obstacle at (3,2) gives a 6-move walk: (0,0) → (0,1) → (0,2) → (1,2) → (2,2) → (3,2) → (4,2).

Example 2
Input: grid = [[0,1,1],[1,1,1],[1,0,0]], k = 1
Output: -1

Reaching the end needs at least two removals, but only one is allowed.

Constraints
  • • 1 ≤ m, n ≤ 40
  • • 1 ≤ k ≤ m · n
  • • Each cell is 0 or 1.
  • • grid[0][0] = grid[m − 1][n − 1] = 0
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“remove up to k obstacles”
→ The removals are a budget. A state has to carry how many are left.
“fewest moves”
→ Every move costs one, so BFS gives the fewest moves. The state is bigger than the cell alone.
“up to k removals”
→ More removals left is never worse. That rule lets the search skip weaker states.
03

Approach Complexity Ladder

Brute Force — DFS over paths, carrying the removals leftO(3^(m · n))O(m · n)

DFS over simple paths; each path carries how many obstacles it may still remove

Optimal — BFS over (cell, removals left), keep the best per cellO(m · n · k)O(m · n · k)

BFS over states (cell, removals left); skip a state that a better one already covers

Brute Force Intuition

Walk every simple path from the top-left cell, carrying the number of removals left. Stepping onto an obstacle uses one removal, and a path with none left is cut. Keep the fewest moves that reach the end. Every path is walked on its own, so the work is exponential.

Recognition rule for this sub-pattern:

Unweighted grid → BFS. Extra state (keys, obstacles removed) becomes part of the node.
Remember this · Graph BFS / DFS
Trigger
Grid of cells, islands, regions, "minimum steps", transformations between states, connected components.
Think
What is a node, what is an edge, and do I need the shortest path or just reachability?
Key question
How do I mark visited so I never process a node twice?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.