286. Walls and Gates

Medium
You are given a m x n 2D grid initialized with these three possible values.

-1 - A wall or an obstacle.
0 - A gate.
INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.

For example, given the 2D grid:

INF  -1  0  INF
INF INF INF  -1
INF  -1 INF  -1
  0  -1 INF INF

After running your function, the 2D grid should be:

  3  -1   0   1
  2   2   1  -1
  1  -1   2  -1
  0  -1   3   4

这道题看起来就知道要用BFS,但是写起来还是卡住了,不知道该怎么像number of islands那样一个一个吃掉岛,一层一层蔓延来计算到gate的最短距离。看了答案还是觉得写得比较巧妙,把关注点放在了gate身上而不是一个一个INFI也就是empty space. 再次重复一下最基本的东西:BFS用Queue, BFS用Queue, BFS用Queue.

我们先把所有的gate offer到queue里,然后开始poll. 我们每次poll()的时候,要观察上下左右四个方向的元素(BFS里常见的步骤),如果元素为INFI,也就是empty rooms,那么我们就知道了该点到gate的最短距离肯定是1(因为我们从gate出发,向上下左右四个方向只走了1步)。那么我们更新该点的值为rooms[i][j] + 1.其实这时候就是更新为1, 但为什么我们不直接写1呢?后面即将揭晓。更新完之后,我们要把该点offer到queue里。实际上,该点就构成了其他点通向gate的一条path。

当我们把gate都poll完之后,继续poll出来的点就是刚才我们遇到的INFI更新后的点。当我们poll他们时,同样也继续访问他们周围的四个点,如果遇到INFI也就是empty space, 我们继续更新周围这些点到gate的距离。这时候就应该更新为rooms[i][j] + 1而不是1. 因为现在的中心点并不是gate,也是可以通往gate的empty space.这个周围的点要通往gate,就需要先到这个中心点,再走到gate,所以是rooms[i][j] + 1.

class Solution {
    //shortest path
    public void wallsAndGates(int[][] rooms) {
        if (rooms == null || rooms.length == 0){
            return;
        }
        int n = rooms.length;
        int m = rooms[0].length;
        Queue queue = new LinkedList<>();
        //first offer all gates to the queue
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                if (rooms[i][j] == 0){
                    queue.offer(new int[] {i, j});
                }
            }
        }
        while (!queue.isEmpty()){
            int[] curt = queue.poll();
            int row = curt[0];
            int col = curt[1];
            //first, check the up, down, left, right of the gate, if the value of that grid is INF, we know now the closest 
            //path to a gate is 1, or in general rooms[i][j] + 1. Then we add this grid to the queue, it's not IFNI anymore.
            //When we poll out this grid to the queue, we will check its up, down, left, right, and if the value of these
            //grid is IFNI, we know we can get to the gate from this spot through what we polled out from the queue, and the 
            //total path is just 1 more. 
            
            if (row > 0 && rooms[row - 1][col] == Integer.MAX_VALUE){
                rooms[row - 1][col] = rooms[row][col] + 1;
                queue.offer(new int[]{row - 1, col});
            }
            if (row < n - 1 && rooms[row + 1][col] == Integer.MAX_VALUE){
                rooms[row + 1][col] = rooms[row][col] + 1;
                queue.offer(new int[]{row + 1, col});
            }
            if (col > 0 && rooms[row][col - 1] == Integer.MAX_VALUE){
                rooms[row][col - 1] = rooms[row][col] + 1;
                queue.offer(new int[]{row, col - 1});
            }
            if (col < m - 1 && rooms[row][col + 1] == Integer.MAX_VALUE){
                rooms[row][col + 1] = rooms[row][col] + 1;
                queue.offer(new int[]{row, col + 1});
            }
        }
    }
}

这道题还可以用DFS做,先遍历整个rooms, 找到所有的gate进行DFS搜索。搜索前进行边界判断(DFS常用),注意一下这里的d值


你可能感兴趣的:(286. Walls and Gates)