forked from JojoYang666/Leetcode-301-600
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_317_ShortestDistancefromAllBuildings.java
More file actions
150 lines (127 loc) · 4.26 KB
/
_317_ShortestDistancefromAllBuildings.java
File metadata and controls
150 lines (127 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package leetcode_1To300;
import java.util.LinkedList;
import java.util.Queue;
/**
* 本代码来自 Cspiration,由 @Cspiration 提供
* 题目来源:http://leetcode.com
* - Cspiration 致力于在 CS 领域内帮助中国人找到工作,让更多海外国人受益
* - 现有课程:Leetcode Java 版本视频讲解(1-900题)(上)(中)(下)三部
* - 算法基础知识(上)(下)两部;题型技巧讲解(上)(下)两部
* - 节省刷题时间,效率提高2-3倍,初学者轻松一天10题,入门者轻松一天20题
* - 讲师:Edward Shi
* - 官方网站:https://cspiration.com
* - 版权所有,转发请注明出处
*/
public class _317_ShortestDistancefromAllBuildings {
/**
* 317. 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.
int[][] dist
int[][] nums
for
for
if (grid[i][j] == '1')
BFS
* time: O(m^2 * n^2)
* space: O(mn)
* @param grid
* @return
*/
public int shortestDistance(int[][] grid) {
if (grid == null || grid.length == 0) return -1;
int m = grid.length;
int n = grid[0].length;
int[][] dist = new int[m][n];
int[][] nums = new int[m][n];
int buildingNum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
buildingNum++;
bfs(grid, i, j, dist, nums);
}
}
}
int res = Integer.MAX_VALUE;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0 && dist[i][j] != 0 && nums[i][j] == buildingNum) {
res = Math.min(res, dist[i][j]);
}
}
}
return res == Integer.MAX_VALUE ? -1 : res;
}
/**
1 - 0 - 2 - 0 - 1
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 1 - 0 - 0
visited :
1 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 0 - 0 - 0
dist :
0 - 1 - 0 - 0 - 0
| | | | |
1 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 0 - 0 - 0
nums :
0 - 1 - 0 - 0 - 0
| | | | |
1 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 0 - 0 - 0
queue : (0,0)
queue : (0,1) (1,0)
queue : ()
* @param grid
* @param row
* @param col
* @param dist
* @param nums
*/
private void bfs(int[][] grid, int row, int col, int[][] dist, int[][] nums) {
int m = grid.length;
int n = grid[0].length;
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{row, col});
int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
boolean[][] visited = new boolean[m][n];
int distance = 0;
while (!queue.isEmpty()) {
distance++;
int size = queue.size();
for (int i = 0; i < size; i++) {
int[] cur = queue.poll();
for (int k = 0; k < dirs.length; k++) {
int x = cur[0] + dirs[k][0];
int y = cur[1] + dirs[k][1];
if (x >= 0 && x < m && y >= 0 && y < n && !visited[x][y] && grid[x][y] == 0) {
visited[x][y] = true;
dist[x][y] += distance;
nums[x][y]++;
queue.offer(new int[]{x, y});
}
}
}
}
}
}