Cheapest Acorn Delivery Route for a Chipmunk on a Grid
Math & Bit ManipulationAuto-graded · 20 test casesLast updated
Compute the minimum total steps to ferry every item to a single target cell on an open grid, carrying one at a time, using Manhattan distances and a smart choice of the first pickup.
Cheapest Acorn Delivery Route for a Chipmunk on a Grid
A chipmunk stands on a rectangular grid with rows rows and cols columns. A hollow log sits on one cell, and several acorns are scattered on other cells. The chipmunk has to bring every acorn to the log.
It can carry only one acorn at a time. In a single step it moves to a cell that is directly above, below, left or right of its current cell.
Return the smallest total number of steps needed to bring all acorns to the log.
Input
rows: number of rows in the grid.cols: number of columns in the grid.log: the log's cell as[row, col].chipmunk: the chipmunk's starting cell as[row, col].acorns: a list of acorn cells, each[row, col].
Output
An integer: the minimum total number of steps over all possible delivery orders.
Rules
- Row
0is the top row and column0is the leftmost column. A step moves from(row, col)to(row - 1, col),(row + 1, col),(row, col - 1)or(row, col + 1). The chipmunk never leaves the grid, and there are no obstacles. - The chipmunk starts empty-handed. Picking up an acorn that lies on its current cell, and dropping an acorn at the log, cost zero steps.
- The chipmunk may walk across cells containing other acorns without taking them, and it never holds more than one acorn at once.
- Acorns can be delivered in any order. Counting ends at the moment the last acorn is dropped at the log; no walking afterwards is needed.
Constraints
1 <= rows <= 100and1 <= cols <= 1000 <= row < rowsand0 <= col < colsfor the log, the chipmunk and every acorn.1 <= len(acorns) <= 5000- All acorns are on distinct cells, and none is on the log's cell.
- The chipmunk may start on the log's cell or on a cell containing an acorn.
- The answer is less than
2 * 10^6.
Examples
Example 1
Input: min_delivery_steps(4, 5, [1, 1], [3, 4], [[0, 4], [3, 1]])
Output: 11
Example 2
Input: min_delivery_steps(1, 10, [0, 2], [0, 9], [[0, 0], [0, 4]])
Output: 11
Example 3
Input: min_delivery_steps(3, 3, [1, 1], [0, 0], [[0, 0], [2, 2], [0, 2]])
Output: 10
Hints
Hint 1
Break the whole journey into trips: walk to an acorn, then carry it to the log. After the first delivery, where does the chipmunk always stand?
Hint 2
Without obstacles, the shortest path between two cells is their Manhattan distance, so no search is needed.
Hint 3
If every trip began at the log, the cost would be twice the acorn-to-log distance summed over all acorns. Only the first trip differs; decide which acorn minimizes the change (distance from chipmunk to acorn minus distance from acorn to log), and remember this change can be positive.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Because the chipmunk carries one acorn at a time, the journey splits into trips: go to an acorn, then carry it to the log. Every trip ends at the log, so every trip except the first also starts there.
With no obstacles, the shortest distance between two cells is the Manhattan distance. For each acorn define:
to_log= distance from the acorn to the log,from_start= distance from the chipmunk's starting cell to the acorn.
If all trips started at the log, acorn a would cost 2 * to_log(a). Only the first trip is different: it costs from_start(f) + to_log(f) instead of 2 * to_log(f). Therefore
total = sum(2 * to_log(a)) + (from_start(f) - to_log(f))
where f is the acorn delivered first. The sum is the same for every order, so we simply choose f to minimize from_start - to_log.
Why it is correct
- Lower bound: In any valid plan, split the moves into segments ending at each drop. The first segment starts at the chipmunk's cell and must reach the first acorn and then the log, costing at least
from_start(f) + to_log(f). Each later segment starts at the log and costs at least2 * to_log. - Achievability: With no obstacles, and with walking over other acorns allowed, each leg can follow a Manhattan-shortest path, so the bound is met exactly.
- Optimization: Only the first-acorn term varies, so minimizing it gives the optimum.
- The "stop at the last drop" rule changes nothing, since each trip already ends at the log.
Edge cases
- The best delta can be positive (the chipmunk may be far from everything), so do not initialize it to 0; start from the first acorn's value.
- If the chipmunk starts on the log, every delta is 0.
- If it starts on an acorn, that acorn has
from_start = 0, but another acorn could still give a smaller delta, so take the true minimum.
Complexity
- Time: O(N) for N acorns.
- Space: O(1) extra.
Reference solution
from typing import List
def min_delivery_steps(rows: int, cols: int, log: List[int], chipmunk: List[int], acorns: List[List[int]]) -> int:
lr, lc = log
cr, cc = chipmunk
base = 0 # total if every trip started at the log
best = None # smallest extra cost from choosing a given acorn first
for r, c in acorns:
to_log = abs(r - lr) + abs(c - lc)
from_start = abs(r - cr) + abs(c - cc)
base += 2 * to_log
delta = from_start - to_log
if best is None or delta < best:
best = delta
return base + best