[LeetCode] 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.
解法一:
class Solution { public: int shortestDistance(vector<vector<int>>& grid) { int res = INT_MAX, val = 0, m = grid.size(), n = grid[0].size(); vector<vector<int>> sum = grid; vector<vector<int>> dirs{{0,-1},{-1,0},{0,1},{1,0}}; for (int i = 0; i < grid.size(); ++i) { for (int j = 0; j < grid[i].size(); ++j) { if (grid[i][j] == 1) { res = INT_MAX; vector<vector<int>> dist = grid; queue<pair<int, int>> q; q.push({i, j}); while (!q.empty()) { int a = q.front().first, b = q.front().second; q.pop(); for (int k = 0; k < dirs.size(); ++k) { int x = a + dirs[k][0], y = b + dirs[k][1]; if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == val) { --grid[x][y]; dist[x][y] = dist[a][b] + 1; sum[x][y] += dist[x][y] - 1; q.push({x, y}); res = min(res, sum[x][y]); } } } --val; } } } return res == INT_MAX ? -1 : res; } };
下面這種方法也是網上比較流行的解法,我們還是用BFS來做,其中dist是累加距離場,cnt表示某個位置已經計算過的建築數,變數buildingCnt為建築的總數,我們還是用queue來輔助計算,注意這裡的dist的更新方式跟上面那種方法的不同,這裡的dist由於是累積距離場,所以不能用dist其他位置的值來更新,而是需要直接加上和建築物之間的距離,這裡用level來表示,每遍歷一層,level自增1,這樣我們就需要所加個for迴圈,來控制每一層中的level值是相等的,參見程式碼如下:
解法二:
class Solution { public: int shortestDistance(vector<vector<int>>& grid) { int res = INT_MAX, buildingCnt = 0, m = grid.size(), n = grid[0].size(); vector<vector<int>> dist(m, vector<int>(n, 0)), cnt = dist; vector<vector<int>> dirs{{0,-1},{-1,0},{0,1},{1,0}}; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) { ++buildingCnt; queue<pair<int, int>> q; q.push({i, j}); vector<vector<bool>> visited(m, vector<bool>(n, false)); int level = 1; while (!q.empty()) { int size = q.size(); for (int s = 0; s < size; ++s) { int a = q.front().first, b = q.front().second; q.pop(); for (int k = 0; k < dirs.size(); ++k) { int x = a + dirs[k][0], y = b + dirs[k][1]; if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 0 && !visited[x][y]) { dist[x][y] += level; ++cnt[x][y]; visited[x][y] = true; q.push({x, y}); } } } ++level; } } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 0 && cnt[i][j] == buildingCnt) { res = min(res, dist[i][j]); } } } return res == INT_MAX ? -1 : res; } };
類似題目:
參考資料: