← 返回 bytedance 的题目列表Shortest Path in 2D Array with Obstacle Elimination
类型:online_judge
Given a 2D array grid, where each element is either 0 or 1, with 1 representing an obstacle. You are given a quota k which allows you to eliminate k obstacles. Write an algorithm to find the shortest path from the top-left corner (0, 0) to the bottom-right corner (n-1, m-1). You can only move horizontally or vertically and cannot move outside the boundaries of the array.
Input:
grid: A 2D array where grid[i][j] is either 0 or 1
k: An integer representing the number of obstacles you can eliminate
Output:
Return the shortest path from the top-left to the bottom-right corner; return -1 if no such path exists
Example:
Input: grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1
Output: 6
Input: grid = [[0,1,1],[1,1,1],[1,0,0]], k = 1
Output: -1
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 40
1 <= k <= m * n
Example
Input
grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]]; k = 1