Multi-source Breadth First Search
Breadth-first search
Through a topic地图分析, to explain and practice BFS.
Principle
Consider the simplest method, which is to find each ocean area (grid [i] [j] 0 的区域)的「最近陆地区域」,然后记录下它们的距离,然后在这些距离里面取一个最大值。
For a given area (x, y) (x, y), we can use the Breadth First Search idea to find its “nearest land area”. We take the coordinates of each area and the Manhattan distance from this area to (x, y) (x, y) as the search state, that is, the x, y and step properties of the Coordinate structure. The findNearestLand method implements the process of Breadth First Search. We use a vis [u] [v] array to record whether the (u, v) (u, v) area has been visited. When expanding the new state, follow the following four directions:
(x - 1, y)(x−1,y)
(x, y + 1)(x,y+1)
(x + 1, y)(x+1,y)
(x, y - 1)(x,y−1)
Here we can define the four directions as constant delta arrays dx and dy.
Thinking: Do we need to search to find that the queue is empty to stop BFS? The answer is no. When we search for a newly joined area whose grid value is 1, that is, this area is a land area, we can stop the search, because BFS can guarantee that the current area is the nearest land area (the nature of BFS determines that what is found here must be the shortest path).
findNearestLand returns -1 if we can’t find any point that is a land area. Finally we set the initial value of ans to -1 and then take the maximum with all BFS results.
Achieve
The code implementation is as follows.
1 | /** |
The core of the above code is to traverse each ocean node and calculate the distance to the nearest land node. This calculation process uses BFS. If the search is completed and cannot be found, it will return -1.
Multi-source BFS
Principle
The so-called multi-source bfs is actually to create a virtual super source point on the basis of bfs. This source point is connected to all nodes that you want to use as multi-source points. At the beginning, according to the single-source idea, we should The super source point is added to the queue, and then when the super source point is queued, all the actual source points enter the queue. This effect is the same as adding all source points to the queue at the beginning.
Achieve
title: https://leetcode-cn.com/problems/01-matrix/
1 | /** |