Walls and Gates

题目链接:https://leetcode.com/problems...

这道题感觉是那道“Shortest Distance from All Buildings”的简化版,思路都是一样的。链接:https://segmentfault.com/a/11...

public class Solution {
    public void wallsAndGates(int[][] rooms) {
        /* bfs for each gates, room[i][j] = distance from (i, j) to gate
         * update room[i][j] each time 
         * bfs: 1. q<int[]> and level 
         *      2. go over current level
         *         if (x, y) is in matrix && empty && room[x][y] > level
         *         - room[x][y] = level 
         *         - q.offer(x, y)
         */
         
         for(int i = 0; i < rooms.length; i++) {
             for(int j = 0; j < rooms[0].length; j++) {
                 if(rooms[i][j] == 0) bfs(rooms, i, j);
             }
         }
    }
    
    int[] dx = new int[] {-1, 1, 0, 0};
    int[] dy = new int[] {0, 0, -1, 1};
    private void bfs(int[][] rooms, int x, int y) {
        Queue<int[]> q = new LinkedList();
        q.offer(new int[] {x, y});
        int level = 0;
        // 1. bfs loop use a queue
        while(!q.isEmpty()) {
            level++;
            // 2. go over current level
            for(int j = q.size(); j > 0; j--) {
                int[] cur = q.poll();
                // 4 directions
                for(int i = 0; i < 4; i++) {
                    int nx = cur[0] + dx[i], ny = cur[1] + dy[i];
                    // if (x, y) is in matrix && empty && room[x][y] > level
                    if(nx >= 0 && nx < rooms.length && ny >= 0 && ny < rooms[0].length && rooms[nx][ny] > level) {
                        rooms[nx][ny] = level;
                        q.offer(new int[] {nx, ny});
                    }
                }
            }
        }
    }
}

看到discussion里面bfs还有一种写法。是把所有gates的点都先放到q里面,然后一起遍历。这种写法的好处是:保证了每个点都只被放进q一次,不会重复遍历。保证了时间复杂是O(MN), M = rooms.length, N = rooms[0].length。

public class Solution {
    public void wallsAndGates(int[][] rooms) {
        /* bfs */
        Queue<int[]> q = new LinkedList();
        for(int i = 0; i < rooms.length; i++) {
            for(int j = 0; j < rooms[0].length; j++) {
                if(rooms[i][j] == 0) q.offer(new int[] {i, j});
            }
        }
        bfs(rooms, q); 
    }
    
    int[] dx = new int[] {-1, 1, 0, 0};
    int[] dy = new int[] {0, 0, -1, 1};
    private void bfs(int[][] rooms, Queue<int[]> q) {
        int level = 0;
        while(!q.isEmpty()) {
            level++;
            for(int j = q.size(); j > 0; j--) {
                int[] cur = q.poll();
                // 4 directions
                for(int i = 0; i < 4; i++) {
                    int nx = cur[0] + dx[i], ny = cur[1] + dy[i];
                    if(nx >= 0 && nx < rooms.length && ny >= 0 && ny < rooms[0].length && rooms[nx][ny] > level) {
                        rooms[nx][ny] = level;
                        q.offer(new int[] {nx, ny});
                    }
                }
            }
        }
    }
}

可以不写成层次遍历的形式,直接bfs,level = poll + 1:

public class Solution {
    public void wallsAndGates(int[][] rooms) {
        /* bfs */
        Queue<int[]> q = new LinkedList();
        for(int i = 0; i < rooms.length; i++) {
            for(int j = 0; j < rooms[0].length; j++) {
                if(rooms[i][j] == 0) q.offer(new int[] {i, j});
            }
        }
        bfs(rooms, q); 
    }
    
    int[] dx = new int[] {-1, 1, 0, 0};
    int[] dy = new int[] {0, 0, -1, 1};
    private void bfs(int[][] rooms, Queue<int[]> q) {
        while(!q.isEmpty()) {
            int[] cur = q.poll();
            int x = cur[0], y = cur[1];
            // 4 directions
            for(int i = 0; i < 4; i++) {
                int nx = cur[0] + dx[i], ny = cur[1] + dy[i];
                if(nx >= 0 && nx < rooms.length && ny >= 0 && ny < rooms[0].length && rooms[nx][ny] > rooms[x][y] + 1) {
                    rooms[nx][ny] = rooms[x][y] + 1;
                    q.offer(new int[] {nx, ny});
                }
            }
            
        }
    }
}

dfs的程序:

public class Solution {
    public void wallsAndGates(int[][] rooms) {
        /* dfs */
         for(int i = 0; i < rooms.length; i++) {
             for(int j = 0; j < rooms[0].length; j++) {
                 if(rooms[i][j] == 0) dfs(rooms, i, j, 0);
             }
         }
    }
    
    int[] dx = new int[] {-1, 1, 0, 0};
    int[] dy = new int[] {0, 0, -1, 1};
    private void dfs(int[][] rooms, int x, int y, int depth) {
        for(int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if(nx >= 0 && nx < rooms.length && ny >= 0 && ny < rooms[0].length && rooms[nx][ny] > depth + 1) {
                rooms[nx][ny] = depth + 1;
                dfs(rooms, nx, ny, depth + 1);
            }
        }
    }
}

lulouch13
13 声望6 粉丝