Shortest Path in a Grid with Obstacles Elimination

HardMatrix

Description

Given a grid with obstacles, find the shortest path from top-left to bottom-right. You may eliminate at most k obstacles. Return path length or -1.

Examples

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

Remove one obstacle.

Input:grid = [[0]], k = 0
Output:0
Explanation:

With a 1x1 grid containing no obstacle, the source equals the destination, so the path length is 0.

Input:grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]], k = 2
Output:5
Explanation:

The shortest path is 5 steps, e.g. by going via the clear bottom row.

Constraints

  • 1 ≤ m, n ≤ 40

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!