每天两道LeetCodeHard:(42)

129 阅读1分钟

296. Best Meeting Point

bfs经典题目

题干:

A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|

解释:

给了一个地图,让找地图上的哪个点到所有的起点的距离最小。

思考:

bfs尤其擅长处理距离场的问题。这个题就是这样,使用一个dp矩阵来存储所有点到每个起点的距离和。每一个起点都是一个原点,更新距离场,然后遍历dp找最小值即可。

答案:

答案补充: