HardArrayBreadth-First SearchMatrixUpdated Sep 2026

1293. Shortest Path in a Grid with Obstacles Elimination

Asked at Databricks, DE Shaw

Difficulty

Hard

Time

O(m * n * k)

Space

O(m * n * k)

Pattern

Array

Companies

2

Problem

You are given an m x n grid. Each cell is 0 (open) or 1 (an obstacle). You start at the top-left cell (0, 0) and want to reach the bottom-right cell (m-1, n-1). In one step you move up, down, left or right to a neighboring cell.

Normally you cannot step on a 1. But you are allowed to destroy at most k obstacles along the way. Stepping onto a 1 costs one of your k destructions. Stepping onto a 0 costs nothing.

Return the minimum number of steps to reach the bottom-right cell while destroying at most k obstacles. If no such walk exists, return -1. The start and end cells are always open (0).

Walk from corner to corner, breaking at most k walls

grid01201234000110000011000Start (green), goal (green), and the one wall we break(orange)(0,0) → (0,1) → (0,2)down to (1,2) → (2,2)break wall (3,2)reach (4,2) in 6 stepsThe chosen route uses exactly one destruction and 6moves
With k = 1 you may step on one 1-cell. The best route breaks the wall at (3,2) and takes 6 steps.

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

Examples

Example 1
Inputgrid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1
Output6
WhyIf you never break a wall, the only way around the middle block is very long (10 steps). With one destruction you can break the wall at (3,2) and walk (0,0) → (0,1) → (0,2) → (1,2) → (2,2) → (3,2) → (4,2). That is 6 steps, and only one obstacle was destroyed.
Example 2
Inputgrid = [[0,1,1],[1,1,1],[1,0,0]], k = 1
Output-1
WhyThe start (0,0) is surrounded by walls on its right and below. To escape you must break at least two walls, but k = 1, so no valid walk exists.

Asked At

2 companies
CompanyDifficulty
DatabricksHardView all Databricks questions →
DE ShawHardView all DE Shaw questions →

How to Think About It

1

Brute force: Try all possible paths using DFS and track how many obstacles each path eliminates, keeping the minimum length.

2

Improved: Use BFS but track (row, col, obstacles_used) as state to avoid revisiting the same cell with more or equal obstacles used.

3

Better: BFS with a 3D visited array visited[r][c][k] where k tracks remaining eliminations.

4

Refined: Since BFS explores level by level, the first time you reach the target with any remaining eliminations is guaranteed to be shortest.

5

Optimal: BFS with state (r, c, remaining_k). Use a visited set of (r, c, remaining_k) tuples. Time O(mnk), space O(mnk).

Optimal Approach

Use BFS where each state is (row, col, remaining_k). Start from (0, 0, k) and explore all four directions. If the next cell is an obstacle, only proceed if remaining_k > 0, decrementing k. If the next cell is open, proceed without changing k. Maintain a visited set of (r, c, remaining_k) tuples to avoid cycles. BFS guarantees the first time you reach (m-1, n-1) is the shortest path. Return the distance at that point. Time complexity is O(m * n * k) since each state is visited at most once.

BFS over states (row, col, remaining_k)

layer 0012012340··············Start state (0,0, k=1) with distance 0after 2 steps01201234000··0·········Frontier cells reached with k still = 1, distance 2break wall01201234000110000011000Stepping on (3,2) drops k to 0; then (4,2) is poppedat distance 6state = (r, c, remaining_k)next_k = remaining_k - grid[nr][nc]push only if next_k >= 0 and unseenfirst pop of goal = shortest
Each BFS layer is one more step. The first time we pop the goal, that distance is the shortest.
ComplexityTime O(m * n * k)Space O(m * n * k)Each state (row, col, remaining_k) is pushed at most once, and each state does constant work for its four neighbors.

Solution Code

O(m * n * k) time
from collections import deque

def shortestPath(grid, k):
    m, n = len(grid), len(grid[0])
    if k >= m + n - 2:
        return m + n - 2
    visited = set()
    visited.add((0, 0, k))
    queue = deque([(0, 0, k, 0)])
    while queue:
        r, c, remaining, dist = queue.popleft()
        if r == m - 1 and c == n - 1:
            return dist
        for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            nr, nc = r + dr, c + dc
            if 0 <= nr < m and 0 <= nc < n:
                nk = remaining - grid[nr][nc]
                if nk >= 0 and (nr, nc, nk) not in visited:
                    visited.add((nr, nc, nk))
                    queue.append((nr, nc, nk, dist + 1))
    return -1

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Shortest Path in a Grid with Obstacles Elimination problem?

Given an m x n integer grid where 0 represents an open cell and 1 represents an obstacle, and an integer k, find the shortest path from the top-left to the bottom-right corner while eliminating at most k obstacles. The path cannot pass through more than k obstacles.

How do you solve Shortest Path in a Grid with Obstacles Elimination?

The optimal approach runs in O(m * n * k) time and O(m * n * k) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Shortest Path in a Grid with Obstacles Elimination?

Shortest Path in a Grid with Obstacles Elimination is asked at Databricks, DE Shaw. It is a hard difficulty problem.