← 返回 doordash 的题目列表Minimum Total Distance to DashMart
类型:online_judge
doordash
Given a 2D grid, where 0 represents an empty land, 1 represents a DashMart, and 2 represents an obstacle, your task is to find a starting point such that the total distance to all DashMarts is minimized. The condition varies as both DashMart and obstacles can be starting points, but obstacles cannot be passed through while DashMarts can be. Consider the possibility of DashMarts being separated by obstacles. You need to return this minimal distance sum, or return -1 if it is impossible to reach all DashMarts.
Example:
Input: grid = [[0,2,1],[0,1,2],[0,0,0]]
Output: 2
You need to implement an efficient algorithm to solve this problem, with the grid dimensions up to 100x100.
Example
Input
[[0,2,1],[0,1,2],[0,0,0]]