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:

class Solution {
    public int shortestPath(int[][] grid, int k) {
        if(grid == null || grid.length == 0 || grid[0].length == 0){
            return -1;
        }
        
        int m = grid.length;
        int n = grid[0].length;
        
        int [][] visited = new int[m][n];
        for(int [] arr : visited){
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        
        int level = 0;
        LinkedList<int []> que = new LinkedList<>();
        if(grid[0][0] == 0){
            que.add(new int[]{0, 0, 0});
            visited[0][0] = 0;
        }else{
            if(k < 1){
                return -1;
            }
            
            que.add(new int[]{0, 0 ,1});
            visited[0][0] = 1;
        }
        
        int [][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        
        while(!que.isEmpty()){
            int size = que.size();
            while(size-- > 0){
                int [] cur = que.poll();
                if(cur[0] == m - 1 && cur[1] == n - 1){
                    return level;
                }
                
                for(int [] dir : dirs){
                    int x = cur[0] + dir[0];
                    int y = cur[1] + dir[1];
                    if(x < 0 || x >= m || y < 0 || y >= n){
                        continue;
                    }
                    
                    int step = grid[x][y] + cur[2];
                    if(step > k || visited[x][y] <= step){
                        continue;
                    }
                    
                    que.add(new int[]{x, y, step});
                    visited[x][y] = step;
                }
            }
            
            level++;
        }
        
        return -1;
    }
}