KatInterview

Cheapest Acorn Delivery Route for a Chipmunk on a Grid

Math & Bit ManipulationAuto-graded · 20 test cases
MediumAuto-gradedMath & Bit Manipulationgreedymanhattan-distancegrid

Last 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 0 is the top row and column 0 is 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 <= 100 and 1 <= cols <= 100
  • 0 <= row < rows and 0 <= col < cols for 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.