317. Shortest Distance from All Buildings - jiejackyzhang/leetcode-note GitHub Wiki

You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:

Each 0 marks an empty land which you can pass by freely. Each 1 marks a building which you cannot pass through. Each 2 marks an obstacle which you cannot pass through. For example, given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2):

1 - 0 - 2 - 0 - 1
|   |   |   |   |
0 - 0 - 0 - 0 - 0
|   |   |   |   |
0 - 0 - 1 - 0 - 0

The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7.

Note: There will be at least one building. If it is not possible to build such house according to the above rules, return -1.

解题思路: 从每一栋building出发,广度优先搜索,更新该建筑到各块空地的距离。 并且再用两个数组来记录各个空地能够达到的building的数目以及到达所能到的building的总距离。 最后遍历各个空地,能够达到所有building并且总距离最短的,即为造房子的所在。

public class Solution {  
    private int[] dy = {0, 0, -1, 1};  
    private int[] dx = {-1, 1, 0, 0};  
    private void find(int[][] grid, int[][] reach, int[][] total, int y, int x) {  
        int m = grid.length;  
        int n = grid[0].length;  
        int[][] dist = new int[m][n];  
        ArrayDeque<Position> deque = new ArrayDeque<>();  
        deque.add(new Position(y, x, 0));  
        while (!deque.isEmpty()) {  
            Position pos = deque.remove();  
            for(int i = 0; i < 4; i++) {  
                int ny = pos.y + dy[i];  
                int nx = pos.x + dx[i];  
                if (ny < 0 || ny >= m || nx < 0 || nx >= n) continue;  
                if (grid[ny][nx] != 0) continue;  
                if (dist[ny][nx] != 0) continue;  
                Position next = new Position(ny, nx, pos.d + 1);  
                reach[next.y][next.x]++;  
                dist[next.y][next.x] = next.d;  
                total[next.y][next.x] += next.d;  
                deque.add(next);  
            }  
        }  
    }  
    public int shortestDistance(int[][] grid) {  
        if (grid == null || grid.length == 0 || grid[0].length == 0) return -1;  
        int m = grid.length;  
        int n = grid[0].length;  
        int b = 0;  
        for(int i = 0; i < m; i ++) {  
            for(int j = 0; j < n; j++) {  
                if (grid[i][j] == 1) b++;  
            }  
        }  
        int[][] reach = new int[m][n];  
        int[][] total = new int[m][n];  
        for(int i = 0; i < m; i ++) {  
            for(int j = 0; j < n; j++) {  
                if (grid[i][j] != 1) continue;  
                find(grid, reach, total, i, j);  
            }  
        }  
        int min = -1;  
        for(int i = 0; i < m; i ++) {  
            for(int j = 0; j < n; j++) {  
                if (grid[i][j] == 0 && reach[i][j] == b && (min == -1 || total[i][j] < min)) {  
                    min = total[i][j];  
                }  
            }  
        }  
        return min;  
    }  
}  
class Position {  
    int y, x;  
    int d;  
    Position(int y, int x, int d) {  
        this.y = y;  
        this.x = x;  
        this.d = d;  
    }  
} 
⚠️ **GitHub.com Fallback** ⚠️