题目:
You are given a m x n 2D grid initialized with these three possible values.
-1- A wall or an obstacle.0- A gate.INF- Infinity means an empty room. We use the value231 - 1 = 2147483647to representINFas you may assume that the distance to a gate is less than2147483647.Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with
INF.For example, given the 2D grid:
INF -1 0 INF INF INF INF -1 INF -1 INF -1 0 -1 INF INFAfter running your function, the 2D grid should be:
3 -1 0 1 2 2 1 -1 1 -1 2 -1 0 -1 3 4
这篇博客详细介绍了如何解决LeetCode中的286题,即计算二维网格中每个空房间到最近门的距离。文章通过两种方法进行了解析:BFS(广度优先搜索)和DFS(深度优先搜索)。在BFS中,从所有门的位置开始进行扩散计算;在DFS中,从找到的0(门)位置开始向周围扩散。文章还列举了与该题相关的其他LeetCode试题。
最低0.47元/天 解锁文章
2445

被折叠的 条评论
为什么被折叠?



