Shortest Path in a Grid with Obstacles Elimination Problem
Description
LeetCode Problem 1293.
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.
Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.
Example 1:

Input: grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1
Output: 6
Explanation:
The shortest path without eliminating any obstacle is 10.
The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (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
Explanation: We need to eliminate at least two obstacles to find such a walk.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 401 <= k <= m * ngrid[i][j]is either0or1.grid[0][0] == grid[m - 1][n - 1] == 0
Solution
Approach: BFS With Remaining Eliminations in the State
A plain BFS on cells is not enough: reaching a cell having used fewer eliminations is better, even if it took more steps, because it leaves more freedom later. So the state is (row, col, left), where left is how many obstacles we may still remove. From a state we move to a neighbour (x, y); if it is an obstacle we spend one elimination, so left becomes left - grid[x][y], and the move is only allowed while that stays >= 0.
Every move costs one step, so BFS visits states in order of steps, and the first time we reach the bottom-right corner (with any left) is the shortest walk. If the queue empties first, the answer is -1. We mark each (row, col, left) as seen once, so no state is expanded twice.
A shortcut keeps the state space small. Any path from corner to corner has at least m + n - 2 steps, and a monotone path (only right and down) passes m + n - 3 cells strictly between the corners. If k >= m + n - 3, we can remove every obstacle on such a path, so the answer is m + n - 2 right away. Otherwise k < m + n - 3 <= 77, so there are at most 40 · 40 · 78 states. The shortcut also handles the 1 x 1 grid, where the answer is 0.
Complexity: O(mn · min(k, m + n)) time and space.
from collections import deque
class Solution:
def shortestPath(self, grid: List[List[int]], k: int) -> int:
m, n = len(grid), len(grid[0])
# A monotone path has m + n - 3 cells between the corners; with that many
# eliminations we can walk straight through them.
if k >= m + n - 3:
return m + n - 2
# State: (row, col, eliminations left). seen[i][j][r] marks visited states.
seen = [[[False] * (k + 1) for _ in range(n)] for _ in range(m)]
seen[0][0][k] = True
q = deque([(0, 0, k)])
steps = 0
while q:
steps += 1
for _ in range(len(q)):
i, j, r = q.popleft()
for x, y in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
if 0 <= x < m and 0 <= y < n:
left = r - grid[x][y]
if left < 0 or seen[x][y][left]:
continue
if x == m - 1 and y == n - 1:
return steps
seen[x][y][left] = True
q.append((x, y, left))
return -1