KatInterview

Closest-Pair Dispatch of Couriers on a Walled Grid

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphsbfsgridgreedy

Last updated

Match couriers to parcels by repeatedly choosing the globally closest pair, using BFS shortest paths on a grid with walls and deterministic tie-breaking.

A warehouse floor is modeled as a rectangular grid with some cells walled off. There are k couriers and k parcels, and every courier must end up with exactly one parcel. Assignments are made greedily: the courier-parcel pair that is closest overall is matched first, then the closest pair among those still unmatched, and so on.

Implement a function that returns, for each courier, the index of the parcel it is given.

Input

  • grid: a 2D list of integers. grid[r][c] == 0 means the cell is open and grid[r][c] == 1 means it is a wall.
  • robots: a list of k positions [r, c]. robots[i] is the starting cell of courier i.
  • items: a list of k positions [r, c]. items[j] is the cell of parcel j.

(The parameter names keep "robots" for couriers and "items" for parcels.)

Distance

A courier moves one step at a time to an adjacent cell (up, down, left or right). It cannot leave the grid or step onto a wall. The distance between courier i and parcel j is the fewest steps needed to get from the courier's cell to the parcel's cell. With no walls this is the Manhattan distance abs(r1 - r2) + abs(c1 - c2).

Assignment process

Work in rounds. In each round:

  1. Look at every pair made of an unassigned courier i and an unassigned parcel j.
  2. Choose the pair with the smallest distance.
  3. If several pairs share that smallest distance, choose the one with the smallest courier index i. If still tied, choose the one with the smallest parcel index j.
  4. Assign parcel j to courier i, and take both out of later rounds.

Continue until all couriers are assigned. Since the counts are equal, every parcel is used exactly once.

Output

Return a list result of length k where result[i] is the index of the parcel assigned to courier i.

Constraints

  • 1 <= m, n <= 50, where m = len(grid) and n = len(grid[0]); all rows have length n.
  • Each grid[r][c] is 0 or 1.
  • 1 <= k <= 50 and len(robots) == len(items) == k.
  • Every position satisfies 0 <= r < m and 0 <= c < n, and every courier and parcel is on an open cell.
  • Courier positions are pairwise distinct, and parcel positions are pairwise distinct. A courier and a parcel may share a cell (distance 0).
  • Every parcel is reachable from every courier, so all distances are defined.

Examples

Example 1

Input: assign_couriers([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0], [0, 3]], [[0, 2], [2, 0]])

Output: [1, 0]

Example 2

Input: assign_couriers([[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 1], [2, 1]], [[1, 1], [1, 0]])

Output: [0, 1]

Example 3

Input: assign_couriers([[0, 0, 0], [1, 1, 0], [0, 0, 0]], [[0, 0], [2, 2]], [[2, 0], [0, 2]])

Output: [1, 0]

Hints

Hint 1

Walls break Manhattan distance. Which standard algorithm gives exact shortest step counts on an unweighted grid, and how many times do you need to run it?

Hint 2

Once all k*k distances are known, each round picks the smallest (distance, courier index, parcel index) among free pairs. Think about what happens if you list all such triples in sorted order.

Hint 3

Scan the sorted triples once and accept a triple only if both its courier and its parcel are still unassigned. Prove by induction that this matches the round-by-round rule.