Shortest Grid Path with Obstacle Elimination
Problem statement
You are given an m x n binary grid. A value of 0 is an open cell, and a value of 1 is an obstacle.
Start at the top-left cell (0, 0). In one step, you may move one cell up, down, left, or right while remaining inside the grid. You may eliminate at most k obstacles by entering their cells.
Return the minimum number of steps needed to reach the bottom-right cell (m - 1, n - 1). Return -1 when no valid path exists.
Function
shortestPathWithObstacleEliminations(grid: int[][], k: int) → intExamples
Example 1
grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]]k = 1return = 6A shortest route uses six steps and eliminates one obstacle.
Example 2
grid = [[0,1,1],[1,1,1],[1,0,0]]k = 1return = -1Every route to the destination enters more than one obstacle, so the elimination budget is insufficient.
Example 3
grid = [[0]]k = 0return = 0The start is already the destination, so no step is needed.
Constraints
1 <= m, n <= 40grid[i][j]is0or1.grid[0][0] = grid[m - 1][n - 1] = 00 <= k <= m * n