KatInterview

Signal Propagation Time to All Destinations

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphsbreadth-first-searchgridmulti-source-bfs

Last updated

Tests understanding of multi-source BFS to compute the longest shortest path from any source to all required destinations on a grid with obstacles.

You're working with a rectangular grid that models signal propagation. The grid has m rows and n columns, where each cell contains one of four values:

  • 'S': A transmitter that begins broadcasting at time 0.
  • 'T': A destination that must receive the broadcast.
  • 'B': A barrier that completely blocks signal transmission.
  • '.': Open space through which signals freely travel.

All transmitters begin broadcasting simultaneously at time 0. During each time unit, signals expand from their current positions to all four adjacent cells (up, down, left, right), as long as those cells are within the grid boundaries and are not barriers. Signals pass through both open spaces and destinations without obstruction.

Your task is to determine the minimum time required for signals to reach all destination cells. If the grid contains no destinations, return 0. If any destination is unreachable from all transmitters, return -1.

Constraints

  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 500
  • Each grid[r][c] is one of 'S', 'T', 'B', or '.'

Examples

Example 1

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

Output: 4

Example 2

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

Output: -1

Example 3

Input: signal_propagation_time([['S', '.'], ['.', '.']])

Output: 0

Hints

Hint 1

Think about starting from all transmitters at once rather than running separate searches from each one.

Hint 2

The answer is determined by whichever destination takes the longest to reach, not the first one.

Hint 3

Mark cells as visited when you add them to the queue, not when you process them, to avoid processing the same cell multiple times.