← 返回 bytedance 的题目列表Multi-source BFS Implementation
类型:online_judge
Given a m x n 2D grid where each cell is an empty land, a wall, or a gate. Empty lands are represented by INFINITY, walls by -1, and gates by 0. Implement an algorithm to find the shortest distance from each empty land to the nearest gate. If it's unreachable to any gate, keep its value as INFINITY. Use multi-source Breadth-First Search (BFS) in your solution.
Example:
Input:
[[0, -1, INFINITY, INFINITY],
[INFINITY, INFINITY, -1, 0],
[INFINITY, -1, INFINITY, -1],
[0, -1, INFINITY, INFINITY]]
Output:
[[0, -1, 3, 4],
[1, 2, -1, 0],
[2, -1, 1, -1],
[0, -1, 2, 3]]
Data scale: 1 <= m, n <= 1000.
Example
Input
[[0, -1, INFINITY, INFINITY], [INFINITY, INFINITY, -1, 0], [INFINITY, -1, INFINITY, -1], [0, -1, INFINITY, INFINITY]]