Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Walls and Gates

Problem in Plain English

Rooms hold walls, gates, and empty cells marked with a huge INF value. Fill every empty cell with its distance to the nearest gate, leaving cells no gate can reach as INF.

Problem Statement

You are given an m x n grid rooms holding three possible values: -1 for a wall, 0 for a gate, and 2147483647 (INF) for an empty room. Fill each empty room with its distance to the nearest gate.

If no gate can reach an empty room, leave it as INF. Modify the grid in place and do not return anything.

Constraints

  • m == rooms.length
  • n == rooms[i].length
  • 1 <= m, n <= 250
  • rooms[i][j] is -1, 0, or 2147483647.

Examples

Example 1

Input
rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
Output
[[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]

Cell (0,0) is three steps from gate (3,0) through (2,0) and (1,0). Cell (3,3) is four steps from gate (0,2) through (1,2), (2,2), and (3,2); the bottom gate needs six steps because walls force a detour. Gates and walls retain their original values.

Example 2

Input
rooms = [[-1]]
Output
[[-1]]

The single cell is a wall, so nothing changes.

Example 3

Input
rooms = [[0,2147483647,-1,2147483647],[2147483647,2147483647,-1,2147483647]]
Output
[[0,1,-1,2147483647],[1,2,-1,2147483647]]

The gate reaches the left component at distances 1, 1, and 2. The full wall column separates both right-hand rooms from the gate, so those rooms remain INF even though they are close geometrically.

Example 4

Input
rooms = [[0,2147483647,2147483647,2147483647,0]]
Output
[[0,1,2,1,0]]

Two gates approach the middle room from opposite ends. Its nearest distance is 2, regardless of which wave arrives first; the room next to the right gate gets 1 rather than 3 from the left gate.

Intuition

Each legal up, down, left or right move costs one step, so rooms and gates form an unweighted graph with walls removed. Breadth-first search visits positions in increasing distance. In Example 1, the bottom gate reaches (0,0) in three steps, while the top gate reaches (3,3) in four. Searching separately from each room works, but starting one queue at every gate shares those corridor walks. A room first reached by any gate has already found its nearest gate; unreachable rooms never change.

Approaches

BFS From Every Room

Solution Details

Reveal BFS From Every Room: intuition, complexity, and code

Hints

Hint 1
Searching outward from every empty room re-walks the same corridors to the same gates, so look for one search that serves all rooms.
Hint 2
Waves leaving every gate at the same speed arrive at each room along the shortest route, which suggests seeding the search at all gates at once.
Hint 3
Stamp a room only while it still holds INF: the first arrival is the nearest gate, and later waves must not overwrite it.

Edge Cases

  • A grid with no gates at all, where every empty room correctly keeps 2147483647.
  • A grid with no empty rooms, where the sweep finds nothing to stamp and changes nothing.
  • An empty room sealed behind walls, which no wave reaches and which keeps 2147483647.
  • Two gates equally near a room produce the same distance; queue ordering must not change the output.
  • A single-row or single-column corridor exercises all boundary checks. The farthest finite distance is bounded by the number of non-wall cells minus one.

Common Mistakes and Interview Tips

  • Searching from each empty room instead of once from all gates, which repeats corridor walks and times out on large grids.
  • Re-stamping rooms that already hold a distance, letting a slower wave overwrite the shortest arrival.
  • Walking through walls by checking only bounds and forgetting the -1 guard.
  • In a BFS from each room, treating previously filled positive rooms as blocked disconnects corridors needed by later searches.
  • Adding one to INF can overflow Java int. Only expand gates or rooms already assigned a finite distance.
  • Interview tip: in-place output does not mean constant auxiliary space; the queue can still need O(mn). The input contract is an original grid of -1, 0, and INF values.

Key Takeaway

When every target needs its distance to the nearest of many sources, start one joint search at all sources and stamp each target on first arrival.