|
51 | 51 |
|
52 | 52 | <!-- 这里可写通用的实现逻辑 -->
|
53 | 53 |
|
| 54 | +BFS。 |
| 55 | + |
| 56 | +记 total 变量表示建筑物(`grid[i][j] = 1`)的个数,`cnt[i][j]` 表示空地 `(i, j)` 上能到达的建筑物数量;`dist[i][j]` 表示空地 `(i, j)` 到每个建筑物的距离之和。求解的是满足 `cnt[i][j] == total` 的空地距离和的最小值。 |
| 57 | + |
54 | 58 | <!-- tabs:start -->
|
55 | 59 |
|
56 | 60 | ### **Python3**
|
57 | 61 |
|
58 | 62 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
59 | 63 |
|
60 | 64 | ```python
|
61 |
| - |
| 65 | +class Solution: |
| 66 | + def shortestDistance(self, grid: List[List[int]]) -> int: |
| 67 | + m, n = len(grid), len(grid[0]) |
| 68 | + q = deque() |
| 69 | + total = 0 |
| 70 | + cnt = [[0] * n for _ in range(m)] |
| 71 | + dist = [[0] * n for _ in range(m)] |
| 72 | + for i in range(m): |
| 73 | + for j in range(n): |
| 74 | + if grid[i][j] == 1: |
| 75 | + total += 1 |
| 76 | + q.append((i, j)) |
| 77 | + d = 0 |
| 78 | + vis = set() |
| 79 | + while q: |
| 80 | + d += 1 |
| 81 | + for _ in range(len(q), 0, -1): |
| 82 | + r, c = q.popleft() |
| 83 | + for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]: |
| 84 | + x, y = r + a, c + b |
| 85 | + if 0 <= x < m and 0 <= y < n and grid[x][y] == 0 and (x, y) not in vis: |
| 86 | + cnt[x][y] += 1 |
| 87 | + dist[x][y] += d |
| 88 | + q.append((x, y)) |
| 89 | + vis.add((x, y)) |
| 90 | + ans = float('inf') |
| 91 | + for i in range(m): |
| 92 | + for j in range(n): |
| 93 | + if grid[i][j] == 0 and cnt[i][j] == total: |
| 94 | + ans = min(ans, dist[i][j]) |
| 95 | + return -1 if ans == float('inf') else ans |
62 | 96 | ```
|
63 | 97 |
|
64 | 98 | ### **Java**
|
65 | 99 |
|
66 | 100 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
67 | 101 |
|
68 | 102 | ```java
|
| 103 | +class Solution { |
| 104 | + public int shortestDistance(int[][] grid) { |
| 105 | + int m = grid.length; |
| 106 | + int n = grid[0].length; |
| 107 | + Deque<int[]> q = new LinkedList<>(); |
| 108 | + int total = 0; |
| 109 | + int[][] cnt = new int[m][n]; |
| 110 | + int[][] dist = new int[m][n]; |
| 111 | + int[] dirs = {-1, 0, 1, 0, -1}; |
| 112 | + for (int i = 0; i < m; ++i) { |
| 113 | + for (int j = 0; j < n; ++j) { |
| 114 | + if (grid[i][j] == 1) { |
| 115 | + ++total; |
| 116 | + q.offer(new int[]{i, j}); |
| 117 | + int d = 0; |
| 118 | + boolean[][] vis = new boolean[m][n]; |
| 119 | + while (!q.isEmpty()) { |
| 120 | + ++d; |
| 121 | + for (int k = q.size(); k > 0; --k) { |
| 122 | + int[] p = q.poll(); |
| 123 | + for (int l = 0; l < 4; ++l) { |
| 124 | + int x = p[0] + dirs[l]; |
| 125 | + int y = p[1] + dirs[l + 1]; |
| 126 | + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 0 && !vis[x][y]) { |
| 127 | + ++cnt[x][y]; |
| 128 | + dist[x][y] += d; |
| 129 | + q.offer(new int[]{x, y}); |
| 130 | + vis[x][y] = true; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + int ans = Integer.MAX_VALUE; |
| 139 | + for (int i = 0; i < m; ++i) { |
| 140 | + for (int j = 0; j < n; ++j) { |
| 141 | + if (grid[i][j] == 0 && cnt[i][j] == total) { |
| 142 | + ans = Math.min(ans, dist[i][j]); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + return ans == Integer.MAX_VALUE ? -1 : ans; |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### **C++** |
| 152 | + |
| 153 | +```cpp |
| 154 | +class Solution { |
| 155 | +public: |
| 156 | + int shortestDistance(vector<vector<int>>& grid) { |
| 157 | + int m = grid.size(); |
| 158 | + int n = grid[0].size(); |
| 159 | + typedef pair<int, int> pii; |
| 160 | + queue<pii> q; |
| 161 | + int total = 0; |
| 162 | + vector<vector<int>> cnt(m, vector<int>(n)); |
| 163 | + vector<vector<int>> dist(m, vector<int>(n)); |
| 164 | + vector<int> dirs = {-1, 0, 1, 0, -1}; |
| 165 | + for (int i = 0; i < m; ++i) |
| 166 | + { |
| 167 | + for (int j = 0; j < n; ++j) |
| 168 | + { |
| 169 | + if (grid[i][j] == 1) |
| 170 | + { |
| 171 | + ++total; |
| 172 | + q.push({i, j}); |
| 173 | + vector<vector<bool>> vis(m, vector<bool>(n)); |
| 174 | + int d = 0; |
| 175 | + while (!q.empty()) |
| 176 | + { |
| 177 | + ++d; |
| 178 | + for (int k = q.size(); k > 0; --k) |
| 179 | + { |
| 180 | + auto p = q.front(); |
| 181 | + q.pop(); |
| 182 | + for (int l = 0; l < 4; ++l) |
| 183 | + { |
| 184 | + int x = p.first + dirs[l]; |
| 185 | + int y = p.second + dirs[l + 1]; |
| 186 | + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 0 && !vis[x][y]) |
| 187 | + { |
| 188 | + ++cnt[x][y]; |
| 189 | + dist[x][y] += d; |
| 190 | + q.push({x, y}); |
| 191 | + vis[x][y] = true; |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + int ans = INT_MAX; |
| 200 | + for (int i = 0; i < m; ++i) |
| 201 | + for (int j = 0; j < n; ++j) |
| 202 | + if (grid[i][j] == 0 && cnt[i][j] == total) |
| 203 | + ans = min(ans, dist[i][j]); |
| 204 | + return ans == INT_MAX ? -1 : ans; |
| 205 | + } |
| 206 | +}; |
| 207 | +``` |
| 208 | +
|
| 209 | +### **Go** |
| 210 | +
|
| 211 | +```go |
| 212 | +func shortestDistance(grid [][]int) int { |
| 213 | + m, n := len(grid), len(grid[0]) |
| 214 | + var q [][]int |
| 215 | + total := 0 |
| 216 | + cnt := make([][]int, m) |
| 217 | + dist := make([][]int, m) |
| 218 | + for i := range cnt { |
| 219 | + cnt[i] = make([]int, n) |
| 220 | + dist[i] = make([]int, n) |
| 221 | + } |
| 222 | + dirs := []int{-1, 0, 1, 0, -1} |
| 223 | + for i := 0; i < m; i++ { |
| 224 | + for j := 0; j < n; j++ { |
| 225 | + if grid[i][j] == 1 { |
| 226 | + total++ |
| 227 | + q = append(q, []int{i, j}) |
| 228 | + vis := make([]bool, m*n) |
| 229 | + d := 0 |
| 230 | + for len(q) > 0 { |
| 231 | + d++ |
| 232 | + for k := len(q); k > 0; k-- { |
| 233 | + p := q[0] |
| 234 | + q = q[1:] |
| 235 | + for l := 0; l < 4; l++ { |
| 236 | + x, y := p[0]+dirs[l], p[1]+dirs[l+1] |
| 237 | + if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 0 && !vis[x*n+y] { |
| 238 | + cnt[x][y]++ |
| 239 | + dist[x][y] += d |
| 240 | + q = append(q, []int{x, y}) |
| 241 | + vis[x*n+y] = true |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + } |
69 | 249 |
|
| 250 | + ans := math.MaxInt32 |
| 251 | + for i := 0; i < m; i++ { |
| 252 | + for j := 0; j < n; j++ { |
| 253 | + if grid[i][j] == 0 && cnt[i][j] == total { |
| 254 | + if ans > dist[i][j] { |
| 255 | + ans = dist[i][j] |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + if ans == math.MaxInt32 { |
| 261 | + return -1 |
| 262 | + } |
| 263 | + return ans |
| 264 | +} |
70 | 265 | ```
|
71 | 266 |
|
72 | 267 | ### **...**
|
|
0 commit comments