286. Walls And Gates
Practice Walls And Gates for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
rooms is a grid where -1 is a wall, 0 is a gate, and 2147483647 is an empty room. Fill every empty room, in place, with the number of steps to its nearest gate, moving up, down, left or right and never through walls. Rooms that cannot reach any gate keep the value 2147483647. Return nothing.
Example
rooms = [[INF, -1, 0, INF],
[INF, INF, INF, -1],
[INF, -1, INF, -1],
[ 0, -1, INF, INF]]
-> [[3, -1, 0, 1],
[2, 2, 1, -1],
[1, -1, 2, -1],
[0, -1, 3, 4]]
(INF stands for 2147483647.)
Constraints
- 1 <= m, n <= 100 (m rows, n columns)
- rooms[i][j] is
-1,0or2147483647