Walls and Gates - Problem

Imagine you're designing a smart navigation system for a building! You have an m ร— n grid representing rooms in a building, where each cell can be:

  • -1: A wall or obstacle (impassable)
  • 0: A gate (exit point)
  • INF: An empty room (represented as 2147483647)

Your task is to fill each empty room with the shortest distance to its nearest gate. If a room cannot reach any gate, it should remain INF.

Example: If a room is 3 steps away from the nearest gate, replace INF with 3.

Think of this as calculating evacuation distances - how quickly can someone get from any room to the nearest exit?

Input & Output

example_1.py โ€” Basic Building Layout
$ Input: rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
โ€บ Output: [[3,-1,0,1],[2,1,2,-1],[1,-1,3,-1],[0,-1,2,3]]
๐Ÿ’ก Note: Starting from gates at (0,2) and (3,0), we calculate shortest distances: rooms adjacent to gates get distance 1, rooms 2 steps away get distance 2, etc. Walls (-1) remain unchanged.
example_2.py โ€” Single Gate
$ Input: rooms = [[2147483647,2147483647,0],[2147483647,-1,2147483647]]
โ€บ Output: [[2,1,0],[3,-1,1]]
๐Ÿ’ก Note: With only one gate at (0,2), distances radiate outward: adjacent rooms get 1, rooms 2 steps away get 2, etc. The bottom-left room requires 3 steps to reach the gate.
example_3.py โ€” Unreachable Room
$ Input: rooms = [[0,-1,2147483647]]
โ€บ Output: [[0,-1,2147483647]]
๐Ÿ’ก Note: The rightmost room is completely blocked by the wall and cannot reach the gate, so it remains INF (2147483647).

Visualization

Tap to expand
Multi-Source BFS: Fire Evacuation System3WALLEXIT01212WALL1WALL3WALLEXIT0WALL23Distance waves from EXIT 1Distance waves from EXIT 2๐ŸŽฏ BFS from all exits simultaneously ensures optimal evacuation distances
Understanding the Visualization
1
Identify All Exits
Mark all emergency exits (gates) as starting points with distance 0
2
Simultaneous Expansion
From all exits, simultaneously mark adjacent rooms with distance 1
3
Ripple Effect
Continue expanding outward like ripples, marking rooms with distances 2, 3, 4...
4
Optimal Distances
Each room now shows the shortest evacuation path to any exit
Key Takeaway
๐ŸŽฏ Key Insight: Multi-source BFS from all gates simultaneously guarantees each room gets the shortest possible distance to any exit in optimal O(mn) time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(mn)

Each cell is visited at most once during the BFS traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(mn)

Queue can contain up to mn cells in worst case (all cells are gates)

n
2n
โšก Linearithmic Space

Constraints

  • m == rooms.length
  • n == rooms[i].length
  • 1 โ‰ค m, n โ‰ค 250
  • rooms[i][j] is -1, 0, or 231 - 1
Asked in
Google 28 Amazon 35 Meta 22 Microsoft 18 Apple 15
68.2K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen