KatInterview

Open One Wall to Speed Up Multi-Source Spread

Trees & GraphsAuto-graded · 20 test cases
MediumAuto-gradedTrees & Graphsbfsgridmulti-source-bfs

Last updated

Use multi-source BFS and brute force over single wall removals to minimize the time until every target is reached, with strict-improvement tie-breaking.

Fastest Spread with at Most One Wall Opened

A rectangular map is given as a grid of single-character strings. Each cell is one of:

  • 'S': a source. Every source starts emitting at time 0.
  • 'T': a target. Every target must eventually be reached.
  • 'B': a wall. Nothing can enter or pass through a wall.
  • '.': open floor.

Each time unit, every reached cell passes the signal to its four neighbors (up, down, left, right), as long as the neighbor lies inside the grid and is not a wall. Source and target cells are ordinary passable cells. A cell's arrival time is the earliest time any signal reaches it, and sources have arrival time 0.

The finish time of a grid is the largest arrival time over all target cells. If some target is never reached, that grid has no finish time (it is infeasible).

You may open at most one wall, which turns that single 'B' cell into open floor. You can also open none. Choose the option with the smallest finish time.

Selection rules

  1. Prefer the smallest finish time among all feasible options (opening nothing, or opening any single wall).
  2. If opening nothing is feasible and no single wall opening gives a strictly smaller finish time, the answer is to open nothing.
  3. Otherwise, among the wall openings that reach the best finish time, choose the wall with the smallest row index, and then the smallest column index.
  4. If no option (including opening nothing) reaches all targets, there is no answer.

Function

Implement best_wall_to_open(grid) which returns a tuple (finish_time, (r, c)):

  • finish_time is the best achievable finish time, and (r, c) is the 0-indexed position of the wall to open.
  • If no wall should be opened, return (finish_time, (-1, -1)).
  • If no option reaches all targets, return (-1, (-1, -1)).

Constraints

  • 1 <= m, n <= 30, where m is the number of rows and n the number of columns.
  • 1 <= m * n <= 900
  • Every cell is one of 'S', 'T', 'B', '.'.
  • The grid contains at least one 'S' and at least one 'T'.

Examples

Example 1

Input: best_wall_to_open([['S', '.', 'B', 'T'], ['.', 'B', 'B', '.'], ['.', '.', '.', '.']])

Output: [3, [0, 2]]

Example 2

Input: best_wall_to_open([['S', '.', 'T'], ['.', '.', '.']])

Output: [2, [-1, -1]]

Hints

Hint 1

Fix a grid first: all sources start together at time 0, so one BFS seeded with every source gives each cell's earliest arrival time.

Hint 2

With at most 900 cells there are at most 900 walls, so you can afford one BFS for the unmodified grid plus one BFS per wall. Treat the opened wall as a parameter instead of editing the grid.

Hint 3

Handle selection carefully: when the unmodified grid is feasible, a wall is chosen only if it is strictly faster than that baseline; when the baseline is infeasible, any feasible opening counts, and ties go to the smallest (row, column).