Walls and Gates

Medium

Topics
GraphBFSMatrix

Fill each empty room with the distance to its nearest gate. -1 is a wall, 0 is a gate, and 2147483647 (INF) is an empty room. If a room cannot reach a gate, leave it as INF. Return the filled grid.

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]]

Constraints

  • 1 <= m, n <= 250
  • Each cell is -1, 0, or 2147483647.
Run ⌘' · Submit ⌘⏎