|
2 | 2 |
|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.Deque;
|
| 5 | +import java.util.HashSet; |
5 | 6 | import java.util.LinkedList;
|
6 | 7 | import java.util.List;
|
| 8 | +import java.util.Queue; |
| 9 | +import java.util.Set; |
7 | 10 |
|
8 | 11 | /**
|
9 | 12 | * Date 03/25/2016
|
|
13 | 16 | * https://leetcode.com/problems/shortest-distance-from-all-buildings/
|
14 | 17 | */
|
15 | 18 | public class ShortestDistanceFromAllBuildings {
|
16 |
| - public int shortestDistance(int[][] grid) { |
17 |
| - int countBuilding = 0; |
18 |
| - List<Point> buildings = new ArrayList<>(); |
19 |
| - for (int i = 0; i < grid.length ; i++) { |
20 |
| - for (int j = 0; j < grid[0].length; j++) { |
21 |
| - if (grid[i][j] == 1) { |
22 |
| - countBuilding++; |
23 |
| - buildings.add(new Point(i, j, 0)); |
24 |
| - } |
25 |
| - } |
26 |
| - } |
27 |
| - |
28 |
| - if (countBuilding == 0) { |
29 |
| - return -1; |
| 19 | + private class Point { |
| 20 | + int x, y, dist = 0; |
| 21 | + Point(int row, int col, int dist) { |
| 22 | + x = row; |
| 23 | + y = col; |
| 24 | + this.dist = dist; |
30 | 25 | }
|
| 26 | + } |
31 | 27 |
|
32 |
| - int[][] output = new int[grid.length][grid[0].length]; |
| 28 | + public int shortestDistance(int[][] grid) { |
| 29 | + int totalBuild = 0; |
33 | 30 | int[][] reach = new int[grid.length][grid[0].length];
|
34 |
| - for (Point building : buildings) { |
35 |
| - Deque<Point> queue = new LinkedList<>(); |
36 |
| - queue.offerFirst(building); |
37 |
| - int currentBuildingCount = 0; |
38 |
| - boolean[][] visited = new boolean[output.length][output[0].length]; |
39 |
| - visited[building.x][building.y] = true; |
40 |
| - while (!queue.isEmpty()) { |
41 |
| - Point p = queue.pollLast(); |
42 |
| - output[p.x][p.y] += p.d; |
43 |
| - List<Point> neighbors = neighbors(p, grid.length, grid[0].length); |
44 |
| - for (Point neighbor : neighbors) { |
45 |
| - if (visited[neighbor.x][neighbor.y]) { |
46 |
| - continue; |
| 31 | + int[][] distance = new int[grid.length][grid[0].length]; |
| 32 | + for(int i = 0; i < grid.length; i++) { |
| 33 | + for(int j = 0; j < grid[0].length; j++) { |
| 34 | + if(grid[i][j] == 1) { |
| 35 | + Queue<Point> queue = new LinkedList<>(); |
| 36 | + queue.offer(new Point(i, j, 0)); |
| 37 | + totalBuild++; |
| 38 | + boolean[][] visited = new boolean[grid.length][grid[0].length]; |
| 39 | + while (!queue.isEmpty()) { |
| 40 | + List<Point> neighbors = getNeighbors(queue.poll(), grid, visited, grid.length, grid[0].length); |
| 41 | + for (Point neigh : neighbors) { |
| 42 | + visited[neigh.x][neigh.y] = true; |
| 43 | + reach[neigh.x][neigh.y]++; |
| 44 | + distance[neigh.x][neigh.y] += neigh.dist; |
| 45 | + queue.offer(neigh); |
| 46 | + } |
47 | 47 | }
|
48 |
| - if (grid[neighbor.x][neighbor.y] == 1) { |
49 |
| - currentBuildingCount++; |
50 |
| - } else if (grid[neighbor.x][neighbor.y] != 2) { |
51 |
| - queue.offerFirst(neighbor); |
52 |
| - reach[neighbor.x][neighbor.y] += 1; |
53 |
| - } |
54 |
| - visited[neighbor.x][neighbor.y] = true; |
55 | 48 | }
|
56 | 49 | }
|
57 |
| - if (countBuilding - 1 != currentBuildingCount) { |
58 |
| - return -1; |
59 |
| - } |
60 | 50 | }
|
| 51 | + |
61 | 52 | int min = Integer.MAX_VALUE;
|
62 |
| - for (int i = 0; i < output.length; i++) { |
63 |
| - for (int j = 0; j < output[0].length; j++) { |
64 |
| - if (reach[i][j] == countBuilding) { |
65 |
| - min = Math.min(min, output[i][j]); |
| 53 | + for (int i = 0; i < reach.length; i++) { |
| 54 | + for (int j = 0; j < reach[0].length; j++) { |
| 55 | + if (reach[i][j] == totalBuild) { |
| 56 | + min = Math.min(min, distance[i][j]); |
66 | 57 | }
|
67 | 58 | }
|
68 | 59 | }
|
| 60 | + |
69 | 61 | return min == Integer.MAX_VALUE ? -1 : min;
|
70 | 62 | }
|
71 | 63 |
|
72 |
| - private List<Point> neighbors(Point p, int m, int n) { |
73 |
| - List<Point> neighbors = new ArrayList<>(); |
74 |
| - if (p.y < n - 1) { |
75 |
| - neighbors.add(new Point(p.x, p.y + 1, p.d + 1)); |
76 |
| - } |
77 |
| - if (p.x < m - 1) { |
78 |
| - neighbors.add(new Point(p.x + 1, p.y, p.d + 1)); |
79 |
| - } |
80 |
| - if (p.x > 0) { |
81 |
| - neighbors.add(new Point(p.x - 1, p.y, p.d + 1)); |
82 |
| - } |
83 |
| - if (p.y > 0) { |
84 |
| - neighbors.add(new Point(p.x, p.y - 1, p.d + 1)); |
85 |
| - } |
86 |
| - return neighbors; |
87 |
| - } |
| 64 | + private List<Point> getNeighbors(Point p, int[][] grid, boolean[][] visited, int n, int m) { |
| 65 | + List<Point> resultList = new ArrayList<>(); |
| 66 | + if(p.x > 0 && grid[p.x -1][p.y] == 0 && !visited[p.x - 1][p.y]) |
| 67 | + resultList.add(new Point(p.x -1, p.y, p.dist + 1)); |
| 68 | + if(p.x < n - 1 && grid[p.x + 1][p.y] == 0 && !visited[p.x + 1][p.y]) |
| 69 | + resultList.add(new Point(p.x + 1, p.y, p.dist + 1)); |
| 70 | + if(p.y > 0 && grid[p.x][p.y - 1] == 0 && !visited[p.x][p.y - 1]) |
| 71 | + resultList.add(new Point(p.x, p.y - 1, p.dist + 1)); |
| 72 | + if(p.y < m - 1 && grid[p.x][p.y + 1] == 0 && !visited[p.x][p.y + 1]) |
| 73 | + resultList.add(new Point(p.x, p.y + 1, p.dist + 1)); |
88 | 74 |
|
89 |
| - class Point { |
90 |
| - int x, y, d; |
91 |
| - Point(int x, int y, int d) { |
92 |
| - this.x = x; |
93 |
| - this.y = y; |
94 |
| - this.d = d; |
95 |
| - } |
| 75 | + return resultList; |
96 | 76 | }
|
97 | 77 |
|
98 | 78 | public static void main(String args[]) {
|
99 |
| - int[][] grid = {{1, 0, 2, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 1, 0, 0}}; |
| 79 | + int[][] grid = {{1,1,1,1,1,0},{0,0,0,0,0,1},{0,1,1,0,0,1},{1,0,0,1,0,1},{1,0,1,0,0,1},{1,0,0,0,0,1},{0,1,1,1,1,0}}; |
100 | 80 | int[][] grid1 = {{1,1},{0,1}};
|
101 | 81 | ShortestDistanceFromAllBuildings shortestDistanceFromAllBuildings = new ShortestDistanceFromAllBuildings();
|
102 | 82 | System.out.println(shortestDistanceFromAllBuildings.shortestDistance(grid));
|
|
0 commit comments