Walls and Gates
Given an m x n grid of rooms where -1 is a wall, 0 is a gate, and 2147483647 (INF) is an empty room, fill each empty room with the distance to its nearest gate. If no gate can reach a room, leave it as INF. Modify the grid in place.
Open official problem prompt ↗Label every reachable empty room with its shortest walking distance to the nearest gate.
Imagine every gate releasing water at the same instant; the water floods the hallways one step per second, and each room records the second at which water first arrives.
- 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]]
- Why
- Each empty room is labeled by its shortest 4-directional walking distance to the nearest gate, with walls (-1) blocking paths.
m == rooms.lengthn == rooms[i].length1 <= m, n <= 250rooms[i][j] is -1, 0, or 2147483647