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<int[]> 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值


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容