[LeetCode] 1293. Shortest Path in a Grid with Obstacles Elimination
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.length
n == grid[i].length
1 <= m, n <= 40
1 <= k <= m * n
grid[i][j]
is either0
or1
.grid[0][0] == grid[m - 1][n - 1] == 0
网格中的最短路径。
给你一个 m * n 的网格,其中每个单元格不是 0(空)就是 1(障碍物)。每一步,您都可以在空白单元格中上、下、左、右移动。
如果您 最多 可以消除 k 个障碍物,请找出从左上角 (0, 0) 到右下角 (m-1, n-1) 的最短路径,并返回通过该路径所需的步数。如果找不到这样的路径,则返回 -1 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/shortest-path-in-a-grid-with-obstacles-elimination
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题属于经典的 flood fill 类型的题目,因为求的是最短路径,所以做法还是偏向 BFS。这道题比一般 BFS 需要多考虑的部分是我们有 K 次机会移除障碍物,所以这里我们需要一个三维数组记录当我们行进到矩阵里的某个位置的时候还有多少次机会可以移除障碍物。其余部分跟常规 BFS 类型题目没有区别。
时间O(mn)
空间O(mnk)
Java实现
1 class Solution { 2 int[][] dirs = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; 3 4 public int shortestPath(int[][] grid, int k) { 5 int m = grid.length; 6 int n = grid[0].length; 7 Queue<int[]> queue = new LinkedList<>(); 8 boolean[][][] visited = new boolean[m][n][k + 1]; 9 visited[0][0][0] = true; 10 queue.offer(new int[] {0, 0, 0}); 11 int step = 0; 12 while (!queue.isEmpty()) { 13 int size = queue.size(); 14 for (int i = 0; i < size; i++) { 15 int[] cur = queue.poll(); 16 int x = cur[0]; 17 int y = cur[1]; 18 int curK = cur[2]; 19 if (x == m - 1 && y == n - 1) { 20 return step; 21 } 22 for (int[] dir : dirs) { 23 int newX = x + dir[0]; 24 int newY = y + dir[1]; 25 int newK = curK; 26 if (newX >= 0 && newX < m && newY >= 0 && newY < n) { 27 if (grid[newX][newY] == 1) { 28 newK++; 29 } 30 if (newK <= k && !visited[newX][newY][newK]) { 31 visited[newX][newY][newK] = true; 32 queue.offer(new int[] {newX, newY, newK}); 33 } 34 } 35 } 36 } 37 step++; 38 } 39 return -1; 40 } 41 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2019-11-01 [LeetCode] 278. First Bad Version
2019-11-01 [LeetCode] 53. Maximum Subarray
2019-11-01 [LeetCode] 435. Non-overlapping Intervals
2019-11-01 [LeetCode] 57. Insert Interval
2019-11-01 [LeetCode] 56. Merge Intervals
2019-11-01 [LeetCode] 253. Meeting Rooms II