Shortest Distance from All Buildings

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.

 思路:

从每个点是1的点出发,通过bfs找到这个点到每个0点最短距离,同时记录该0点被1点访问过的次数。这样我们遍历所有1点,对所有能够被访问到的1点,保存最短距离以及增加访问次数。最后把所有0点遍历一遍,看它是否被所有1点访问到,并且最短距离和最小。

其实这里也可以从0出发,做类似的事情。选1还是0看它们的个数。谁小就选谁。

 1 public class Solution {
 2     private int[][] dir = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
 3 
 4     public int shortestDistance(int[][] grid) {
 5         if (grid == null || grid.length == 0) {
 6             return 0;
 7         }
 8 
 9         int rows = grid.length, cols = grid[0].length;
10 
11         int[][] reach = new int[rows][cols];
12         int[][] distance = new int[rows][cols];
13         int numBuildings = 0;
14 
15         // Find the minimum distance from all buildings
16         for (int i = 0; i < rows; i++) {
17             for (int j = 0; j < cols; j++) {
18                 if (grid[i][j] == 1) {
19                     shortestDistanceHelper(i, j, grid, reach, distance);
20                     numBuildings++;
21                 }
22             }
23         }
24 
25         // step 2: check the min distance reachable by all buildings
26         int minDistance = Integer.MAX_VALUE;
27         for (int i = 0; i < rows; i++) {
28             for (int j = 0; j < cols; j++) {
29                 if (grid[i][j] == 0 && reach[i][j] == numBuildings && distance[i][j] < minDistance) {
30                     minDistance = distance[i][j];
31                 }
32             }
33         }
34 
35         if (minDistance == Integer.MAX_VALUE) {
36             return -1;
37         } else {
38             return minDistance;
39         }
40     }
41 
42     private void shortestDistanceHelper(int row, int col, int[][] grid, int[][] reach, int[][] distance) {
43         int rows = grid.length;
44         int cols = grid[0].length;
45         int d = 0;
46         boolean[][] visited = new boolean[rows][cols];
47         Queue<int[]> queue = new LinkedList<>();
48         queue.offer(new int[] { row, col });
49         visited[row][col] = true;
50         while (!queue.isEmpty()) {
51             d++;
52             int size = queue.size();
53             for (int j = 0; j < size; j++) {
54                 int[] cord = queue.poll();
55                 for (int i = 0; i < 4; i++) {
56                     int rr = dir[i][0] + cord[0];
57                     int cc = dir[i][1] + cord[1];
58                     if (isValid(rr, cc, grid, visited)) {
59                         queue.offer(new int[] { rr, cc });
60                         visited[rr][cc] = true;
61                         reach[rr][cc]++;
62                         distance[rr][cc] += d;
63                     }
64                 }
65             }
66         }
67     }
68 
69     private boolean isValid(int row, int col, int[][] grid, boolean[][] visited) {
70         int rows = grid.length;
71         int cols = grid[0].length;
72 
73         if (row < 0 || row >= rows || col < 0 || col >= cols || visited[row][col] || grid[row][col] == 2) {
74             return false;
75         }
76         return true;
77     }
78 }

猜你喜欢

转载自www.cnblogs.com/beiyeqingteng/p/11261755.html