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:


Solution:

class Solution {
    static class Node {
        int x;
        int y;
        int k;
        
        public Node(int x, int y, int k) {
            this.x = x;
            this.y = y;
            this.k = k;
        }
        
        @Override
        public boolean equals(Object o) {
            Node n = (Node) o;
            return this.x == n.x && this.y == n.y && this.k == n.k;
        }
        
        @Override
        public int hashCode() {
            int hash = 17;
            hash = 31 * hash + this.x;
            hash = 131 * hash + this.y;
            hash+= 137 * hash + this.k;
            return hash;
        }
    }
    
    int[] dx = new int[]{-1,0,1,0};
    int[] dy = new int[]{0,1,0,-1};
    
    public int shortestPath(int[][] grid, int k) {
        int m = grid.length, n = grid[0].length;
        Queue<Node> queue = new ArrayDeque();
        Set<Node> visited = new HashSet();
        Node start = new Node(0, 0, k);
        queue.offer(start);
        visited.add(start);
        int step = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i ++) {
                Node curr = queue.poll();
                if (curr.x == m - 1 && curr.y == n - 1) {
                    return step;
                }
                for (int j = 0; j < 4; j ++) {
                    int nx = curr.x + dx[j];
                    int ny = curr.y + dy[j];
                    if (nx >= 0 && nx < m && ny >= 0 && ny < n) {
                        if (grid[nx][ny] == 0) {
                            Node next = new Node(nx, ny, curr.k);
                            if (visited.add(next)) {
                                queue.offer(next);
                            }
                        } else if (curr.k > 0) {
                            Node next = new Node(nx, ny, curr.k - 1);
                            if (visited.add(next)) {
                                queue.offer(next);
                            }
                        }
                    }
                }
            }
            step ++;
        }
        return -1;
    }
}