[leetcode] 317. Shortest Distance from All Buildings解題レポート
タイトルリンク:https://leetcode.com/problems/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
The point
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つの建物から計算し、1つの空き地から他のすべての建物までの距離を集計する配列sumDistanceを設定することができる.すなわちsumDistance[i][j]は位置grid[i][j]から他の建物までの距離の和を表す.
1つの建物からすべての空き地からその距離を探す場合、bfsを用いて計算する、地図の1つのノードにアクセスするたびにその値-1をタグとして使用する、このノードにアクセスしたことをタグとして使用し、次のノードにアクセスできることをタグとして使用することができる.これにより、アクセスしたか否かをマークするもう1つの配列が回避する.
コードは次のとおりです.
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つの建物から計算し、1つの空き地から他のすべての建物までの距離を集計する配列sumDistanceを設定することができる.すなわちsumDistance[i][j]は位置grid[i][j]から他の建物までの距離の和を表す.
1つの建物からすべての空き地からその距離を探す場合、bfsを用いて計算する、地図の1つのノードにアクセスするたびにその値-1をタグとして使用する、このノードにアクセスしたことをタグとして使用し、次のノードにアクセスできることをタグとして使用することができる.これにより、アクセスしたか否かをマークするもう1つの配列が回避する.
コードは次のとおりです.
class Solution {
public:
int shortestDistance(vector<vector<int>>& grid) {
if(grid.size() ==0) return -1;
vector<pair<int, int>> direction{{0,1},{0,-1},{1,0},{-1,0}};
int m = grid.size(), n = grid[0].size(), result=-1, flag=0;
vector<vector<int>> sumDistance(m, vector<int>(n, 0));
for(int i = 0; i < m; i++)
{
for(int j =0; j< n; j++)
{
if(grid[i][j] == 1)
{
result = -1;
queue<pair<int, int>> que;
que.push(make_pair(i, j));
auto tem = grid;
while(!que.empty())
{
auto val = que.front();
que.pop();
for(auto dir:direction)
{
int x = val.first + dir.first, y = val.second+dir.second;
if(x >=0 && x < m && y >=0 && y < n && grid[x][y]==flag)
{
grid[x][y]--;
// , 1, sum tem 1
tem[x][y] = tem[val.first][val.second] + 1;
sumDistance[x][y] += (tem[x][y] -1);
if(result < 0 || result > sumDistance[x][y])
result = sumDistance[x][y];
que.push(make_pair(x, y));
}
}
}
flag--;//
}
}
}
return result;
}
};
参照:https://leetcode.com/discuss/74453/36-ms-c-solution