LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination

原题链接在这里:https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/

题目:

Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell.

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:

  • grid.length == m
  • grid[0].length == n
  • 1 <= m, n <= 40
  • 1 <= k <= m*n
  • grid[i][j] == 0 or 1
  • grid[0][0] == grid[m-1][n-1] == 0

题解:

Use BFS to track the shortest path. In queue, we need coordinate and current count of obstacles eliminated.

For the polled coordinate, if it is the lower right corner, then return the BFS level as step.

Otherwise, check 4 directions, if the new position is wihtin boundary, and new obstacle count <= k, and previous visited obstacle count is > new obstacle count. Add it to the que. And mark visited as current obstacle count.

Time Complexity: O(m * n * k). As for the worst case, each cell is put in and out of queue totally k times.

Space: O(m * n * k).

AC Java:

 1 class Solution {
 2     public int shortestPath(int[][] grid, int k) {
 3         if(grid == null || grid.length == 0 || grid[0].length == 0){
 4             return -1;
 5         }
 6
 7         int m = grid.length;
 8         int n = grid[0].length;
 9
10         int [][] visited = new int[m][n];
11         for(int [] arr : visited){
12             Arrays.fill(arr, Integer.MAX_VALUE);
13         }
14
15         int level = 0;
16         LinkedList<int []> que = new LinkedList<>();
17         if(grid[0][0] == 0){
18             que.add(new int[]{0, 0, 0});
19             visited[0][0] = 0;
20         }else{
21             if(k < 1){
22                 return -1;
23             }
24
25             que.add(new int[]{0, 0 ,1});
26             visited[0][0] = 1;
27         }
28
29         int [][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
30
31         while(!que.isEmpty()){
32             int size = que.size();
33             while(size-- > 0){
34                 int [] cur = que.poll();
35                 if(cur[0] == m - 1 && cur[1] == n - 1){
36                     return level;
37                 }
38
39                 for(int [] dir : dirs){
40                     int x = cur[0] + dir[0];
41                     int y = cur[1] + dir[1];
42                     if(x < 0 || x >= m || y < 0 || y >= n){
43                         continue;
44                     }
45
46                     int step = grid[x][y] + cur[2];
47                     if(step > k || visited[x][y] <= step){
48                         continue;
49                     }
50
51                     que.add(new int[]{x, y, step});
52                     visited[x][y] = step;
53                 }
54             }
55
56             level++;
57         }
58
59         return -1;
60     }
61 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12440920.html

时间: 2024-11-06 21:14:22

LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination的相关文章

leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]

题目链接 Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell. Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right

[LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

An undirected, connected graph of N nodes (labeled?0, 1, 2, ..., N-1) is given as?graph. graph.length = N, and?j != i?is in the list?graph[i]?exactly once, if and only if nodes?i?and?j?are connected. Return the length of the shortest path that visits

BFS 基础写法 —— 以 LeetCode #1091 Shortest Path in Binary Matrix 为例

Question In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that: Adjacent cells C_i and C_{i+1} are connected

[LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径

We are given a 2-dimensional?grid.?"."?is an empty cell,?"#"?is?a wall,?"@"?is the starting point, ("a",?"b", ...) are keys, and ("A",?"B", ...) are locks. We start at the starting poin

[LeetCode 1368] Minimum Cost to Make at Least One Valid Path in a Grid

Given a m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be: 1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1]) 2 

Dijkstra’s Shortest Path Algorithm / LeetCode 787. Cheapest Flights Within K Stops

Dijkstra’s Shortest Path Algorithm 实现详见:https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-priority_queue-stl/ 需要注意的是,priority_queue并无法更新内部的元素,因此我们更新dist的同时,直接把新的距离加入pq即可.pq里虽然有outdated的dist,但是由于距离过长,他们并不会更新dist. // If there is sho

LeetCode --- 64. Minimum Path Sum

题目链接:Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 这道题的要求是在m*n

【Leetcode】Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

leetcode 934. Shortest Bridge

The algorithm for this problem is not so hard to figure out. This is a problem of finding the length of the shortest path in graph. As I mentioned in my previous article, BFS is a good way to slove this kind of problem. This problem is a little diffe