Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode_1293_Shortest Path in a Grid with Obstacles Elimination #49

Open
lihe opened this issue Dec 17, 2019 · 0 comments
Open

Leetcode_1293_Shortest Path in a Grid with Obstacles Elimination #49

lihe opened this issue Dec 17, 2019 · 0 comments
Labels

Comments

@lihe
Copy link
Owner

lihe commented Dec 17, 2019

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

算法思路:利用BFS解题,将二维矩阵扩充为三维,z代表当前消除的障碍物数量;visited[x][y][z],代表消除z个障碍是否到达[x, y]处。

class Solution {
    private int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};


    public int shortestPath(int[][] grid, int k) {
        int m = grid.length;
        int n = grid[0].length;

        boolean[][][] visited = new boolean[m][n][k + 1];
        Queue<Point> queue = new LinkedList<>();

        queue.add(new Point(0, 0, 0));

        visited[0][0][0] = true;

        int distance = 0;

        while (!queue.isEmpty()){
            distance++;

            int size = queue.size();

            for(int i = 0; i < size; i++){
                Point point = queue.poll();

                int x = point.x;
                int y = point.y;
                int z = point.z;

                if(x == m - 1 && y == n - 1){  // 达到目的地
                    return distance - 1;
                }

                for(int j = 0; j < 4; j++){  // 四个方向
                    int new_x = x + dir[j][0];
                    int new_y = y + dir[j][1];
                    int new_z = z;

                    if(new_x < 0 || new_x >= m || new_y < 0 || new_y >= n){  // 超出矩阵范围
                        continue;
                    }

                    if(grid[new_x][new_y] == 1){
                        if ( z < k){  // 可以继续消除障碍物
                            new_z = z + 1;
                        }
                        else{
                            continue;  // 已经到达上限
                        }
                    }

                    if(!visited[new_x][new_y][new_z]){  // 判断是否访问过
                        queue.add(new Point(new_x, new_y, new_z));

                        visited[new_x][new_y][new_z] = true;
                    }
                }
            }
        }
        return -1;
    }

    class Point{
        int x;
        int y;
        int z;

        public Point(int x, int y, int z){
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
}
@lihe lihe added the Leetcode label Dec 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant