317. Shortest Distance from All Buildings

317. Shortest Distance from All Buildings
https://www.youtube.com/watch?v=8K98WexA8m8
 https://leetcode.com/problems/shortest-distance-from-all-buildings/discuss/76891/Java-solution-with-explanation-and-time-complexity-analysis


Line 50: error: illegal start of type






Idea: traverse from the buildings , use a 2d int array to keep track  of the distance from all buildings to this empty land
Also use another 2d int array to keep track of how many buildings have visited this place, 
The output is the one with shortest distance that can reach all buildings 
So we also use a var to count how many buildings in total 
      
      
      
class Solution {
    public int shortestDistance(int[][] grid) {
        int n = grid.length;
        int m = gird[0].length;
        int[][] distance = new int[n][m];
        int[][] count = new int[n][m];

        int numBui = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    bfs(grid, i, j, distance, count, n, m);
                    numBui++;
                }
            }
        }
    }

    private void bfs(int[][] gird, int i, int j, int[][] distance, int[][] count, int n, int m) {
        int[][] directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        boolean[][] visited = new boolean[n][m];
        Queue<int[]> queue = new LinkedList<>();
        int level = 1;
        queue.offer(new int[]{i, j});

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int x = 0; x < size; x++) {
                int[] current = queue.poll();
                for (int[] dir : directions) {
                    int row = current[0] + dir[0];
                    int col = current[1] + dir[1];

                    if (row >= 0 && col >= 0 && row < n && col < m && grid[row][col] == 0 && visited[row][col] == false) {
                        queue.offer(new int[]{row, col});
                        visited[row][col] = true;
                        distance[row][col] += level;
                        count[row][col] += 1;
                    }
                }
            }
            level++;  /////// level++ after visiting all the 0s on the same level
        }
    }
    
    int min = Integer.MAX_VALUE;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(grid[i][j] == 0 && count[i][j] == numBui){
                min = Math.min(min, distance[i][j]);
            }
        }
    }

    

        
    return min == Integer.MAX_VALUE ? -1 : min; //// if doesn’t exists 

        
        
}

 

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.

 

public int shortestDistance(int[][] grid){
    int[][] numBuilding = new int[n][m];
    int n = grid.length;
    int m = grid[0].length;
    int[][] distance = new int[n][m];
    int numBuild = 0;
    
    

    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(gird[i][j] == 1){
                bfs(grid, i, j, distance, n, m, numBuilding);
                numBuild++;
            }
        }
    }

    int res = Integer.MAX_VALUE;
// traverse the grid 
for(int i = 0; i< n; i++){
        for(int j = 0; j < m; j++){
            if(grid[i][j] == 0 && numBuilding[i][j] == numBuild){
                res = Math.min(res, distance[i][j]);
            }
        }
    }
    return res ==  Integer.MAX_VALUE ? Integer.MAX_VALUE : res;
}

private void bfs(int[][] grid, int i, int j, int[][] distance, int n, int m, int[][] numBuilding){
    int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    boolean[][] visited = new boolean[n][m];
    Queue<int[]> queue = new LinkedList<>();
    int level = 1;
    queue.offer(new int[]{i, j});
    
    while(!queue.isEmpty()){
        int size = queue.size();
        for(int x = 0; x < size; x++){
            int[] current = queue.poll();
            int r = current[0];
            int c = current[1];
        
            for(int[] dir: dirs){
                int row = r + dir[0];
                int col = c + dir[1];
            
                // check boundary and if its valid 
                if(row < 0 || col < 0 || row >= n || col >= m || grid[row][col] == 2 || grid[row][col] == 1 || visited[row][col]) continue;
                queue.offer(new int[]{row, col});
                visited[row][col] = true;
                distance[row][col] += level;
                numBuilding[row][col] += 1;
                }
            }
        }
        level++;
    }
}

        


            
    

 

posted on 2018-08-09 19:04  猪猪&#128055;  阅读(194)  评论(0)    收藏  举报

导航