AlgoMaster Logo

Walls and Gates

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We have a 2D grid where some cells are walls (-1), some are gates (0), and the rest are empty rooms (INF). Our job is to update every empty room cell with the shortest distance to the nearest gate. Movement is only allowed in four directions (up, down, left, right), and we cannot pass through walls.

The word "nearest" is what drives the solution. Each empty room needs the minimum distance across all gates, not the distance to one specific gate. One option is to start from each empty room and search outward for the closest gate. The other is to start from the gates and search outward toward the rooms. Starting from the gates turns out to be far cheaper, because a single search from all gates at once fills the entire grid.

That single search is a multi-source BFS. We put all gates into the queue at the start and expand outward from all of them at the same time. Because BFS visits cells in increasing order of distance, the first time it reaches an empty room, the distance recorded is the shortest distance from any gate.

Key Constraints:

  • 1 <= m, n <= 250 → The grid has up to 62,500 cells. A per-room search runs a separate BFS for each of those cells, which is O((m n)^2) in the worst case, around 4 billion operations. A single multi-source pass is O(m n) and stays well within limits.
  • rooms[i][j] is -1, 0, or 2147483647 → Walls, gates, and empty rooms are the only three values, so the empty-room marker (INF) can double as an "unvisited" flag during BFS.

Approach 1: BFS from Each Empty Room

Intuition

Treat each empty room as an independent shortest-path problem. For every cell that contains INF, run a BFS outward until it hits a gate, and record the number of steps it took.

BFS on an unweighted grid expands one distance level at a time, so the first gate reached from a given room is the closest one to that room. That distance is the answer for that room.

Algorithm

  1. Iterate through every cell in the grid.
  2. If the cell is an empty room (value is INF), run a BFS starting from that cell.
  3. In the BFS, explore neighbors in all four directions (up, down, left, right).
  4. Skip walls (-1) and already-visited cells.
  5. The first time we encounter a gate (value 0), that BFS distance is the answer for this room.
  6. Update the room's value with the distance found.

Example Walkthrough

1Initial grid: G=gate, W=wall, .=empty room (INF)
0
1
2
3
0
.
W
gate
G
.
1
.
.
.
W
2
.
W
.
W
3
gate
G
W
.
.
1/7

Code

This approach is correct but runs a full BFS for every empty room, and adjacent rooms re-explore almost the same cells each time. The next approach reverses the direction: it searches outward from the gates and fills the whole grid in one pass.

Approach 2: Multi-Source BFS from All Gates

Intuition

Instead of asking "for each room, what is the closest gate?", flip the question to "from all gates at once, how far is each room?" Adding every gate to the BFS queue at distance 0 and expanding outward fills the entire grid in a single pass.

A multi-source BFS is one BFS seeded with many starting cells. All gates begin at distance 0. The wavefront then advances one step at a time across the whole grid together. The first time the wavefront reaches a room, that room is recorded at the current distance and never touched again.

Algorithm

  1. Create a queue and add the position of every gate (cells with value 0) to it.
  2. Run BFS. For each cell dequeued, explore its four neighbors.
  3. For each neighbor that is an empty room (value is still INF), update its distance to current cell's distance + 1 and add it to the queue.
  4. Skip walls (-1) and cells that have already been updated (value is no longer INF).
  5. When the queue is empty, every reachable empty room has been filled with its shortest distance to any gate.

Example Walkthrough

1Step 0: Find all gates. Queue = [(0,2), (3,0)] at distance 0
0
1
2
3
0
.
W
gate
0
.
1
.
.
.
W
2
.
W
.
W
3
gate
0
W
.
.
1/6

Code