KatInterview

Best Meeting Cell on a Grid with Walls

Trees & GraphsAuto-graded · 23 test cases
MediumAuto-gradedTrees & Graphsbfsgridshortest-path

Last updated

Find the open cell with the smallest total shortest-path distance to a set of target cells on a grid with obstacles, using BFS and reachability counting.

You are given a rectangular grid and a list of target cells. Choose one open cell as a meeting point so that the total walking distance from that cell to every target is as small as possible.

Grid and movement

  • grid is a list of m rows with n integers each.
  • 0 marks an open cell and -1 marks an obstacle.
  • You may move one step up, down, left, or right per move. Each move costs 1.
  • You may only step onto open cells.

Targets

  • targets is a list of [row, col] pairs, using 0-indexed coordinates.
  • The same cell may appear more than once. Each occurrence counts separately in the total.

Task

The meeting cell must be open. It may coincide with a target cell. Its cost is the sum, over all entries of targets, of the shortest walking distance from the meeting cell to that target.

Return a two-element list [best_total, [row, col]], where best_total is the minimum possible cost and [row, col] is the meeting cell that achieves it.

Tie-break: if several cells reach the minimum cost, return the one with the smallest row, and among those the smallest column.

Special cases

  • If the grid has no rows or no columns, return [-1, None].
  • If targets is empty, return [0, [row, col]] for the first open cell in row-major order, or [-1, None] if the grid has no open cell.
  • If any target lies outside the grid or on an obstacle, return [-1, None].
  • If no open cell can reach every target (for example, walls separate the targets), return [-1, None].

Constraints

  • 0 <= m, n and the grid is rectangular (all rows have the same length).
  • Each grid value is either 0 or -1.
  • 0 <= len(targets), and len(targets) is small enough that running a search per target on the grid is feasible.
  • Distances are measured by walking around obstacles, not by straight-line Manhattan distance.

Examples

Example 1

Input: best_meeting_cell([[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0], [2, 2]])

Output: [4, [0, 0]]

Example 2

Input: best_meeting_cell([[0, 0, -1, 0, 0], [0, 0, -1, 0, 0]], [[0, 0], [0, 4]])

Output: [-1, None]

Example 3

Input: best_meeting_cell([[0, 0, 0], [0, -1, 0], [0, 0, 0]], [[0, 0], [2, 2]])

Output: [4, [0, 0]]

Hints

Hint 1

Manhattan distance is wrong once walls exist. You need true shortest path lengths on an unweighted grid, which is what BFS gives you.

Hint 2

Instead of running a BFS from every candidate cell, run one BFS from each distinct target. Each run gives that target's distance to every cell, so you can add these into a running total per cell.

Hint 3

Keep a second array that counts how many targets reached each cell. A cell is a valid meeting point only if it is open and every target reached it. Scan in row-major order with a strict comparison so ties resolve to the smallest cell.