[LeetCode] 317. Shortest Distance from All Buildings

linspiration發表於2019-01-19

Problem

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.
Example:

Input: [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]

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

Output: 7 

Explanation: Given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2),

         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.

Solution

class Solution {
    public int shortestDistance(int[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) return -1;
        int m = grid.length, n= grid[0].length;
        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) count++;
            }
        }
        
        int[][] dist = new int[m][n];
        int[][] conn = new int[m][n];
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    if (!bfs(grid, i, j, count, dist, conn)) return -1;
                }
            }
        }
        
        int min = Integer.MAX_VALUE;
        boolean found = false;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    if (conn[i][j] == count) {
                        found = true;
                        min = Math.min(min, dist[i][j]);
                    }
                }
            }
        }
        return found ? min : -1;
    }
    
    class Point {
        int x;
        int y;
        int distance;
        Point(int x, int y, int dist) {
            this.x = x;
            this.y = y;
            this.distance = dist;
        }
    }
    
    private boolean bfs(int[][] grid, int xstart, int ystart, int count, int[][] dist, int[][] conn) {
        boolean[][] spot = new boolean[grid.length][grid[0].length];
        Deque<Point> queue = new ArrayDeque<>();
        queue.offer(new Point(xstart, ystart, 0));
        spot[xstart][ystart] = true;
        int buildings = 1;
        int[] dx = {-1, 1, 0, 0};
        int[] dy = {0, 0, -1, 1};
        while (!queue.isEmpty()) {
            Point cur = queue.poll();
            for (int i = 0; i < 4; i++) {
                int x = cur.x + dx[i];
                int y = cur.y + dy[i];
                if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) continue;
                //connect to a building
                if (grid[x][y] == 1 && !spot[x][y]) {
                    spot[x][y] = true;
                    buildings++;
                }
                //connect to a spot
                if (grid[x][y] == 0 && !spot[x][y]) {
                    spot[x][y] = true;
                    queue.offer(new Point(x, y, cur.distance+1));
                    dist[x][y] += cur.distance+1;
                    conn[x][y]++;
                }
            }
        }
        return buildings == count;
    }
}

相關文章