[英]Finding shortest distance to water
I have a two-dimensional map of land and water, like the following ( l
representing land and w
representing water). 我有一张土地和水的二维地图,如下图所示( l
代表土地, w
代表水)。
lllwwwlll
lllwllllw
lllllllww
lllllllll
For each land tile, I want to know its distance from the nearest water tile. 对于每块地砖,我想知道它距离最近的水砖的距离。 Moving horizontally, vertically, and diagonally all count as a distance of one. 水平,垂直和对角线移动都计为一个距离。
321www111
321w1111w
3211121ww
322222111
Right now I am using an algorithm like so: 现在我正在使用这样的算法:
foreach tile in map
if isWater(tile)
tile.distanceFromWater = 0
else
radius = 1
while !hasWaterAround(tile, radius)
radius++
tile.distanceFromWater = radius
This approach works, but it rather slow, especially when there are very few water tiles. 这种方法有效,但速度相当慢,特别是在水砖很少的情况下。
Is there a faster algorithm for finding the distance of each land tile from water? 是否有更快的算法来查找每块地砖与水的距离?
We can do something like the following (similar to BFS, but starting possibly with multiple sources): 我们可以做类似以下的事情(类似于BFS,但可能有多个来源):
Have a queue (FIFO) initially empty. 队列(FIFO)最初为空。 Have another mxn grid D of distances with all elements initialized to infinity. 有另一个mxn网格D的距离,所有元素都初始化为无穷大。
Step 2 should not take more than O(mn) time for a mxn grid. 对于mxn网格,步骤2的时间不应超过O(mn)。
Do a breadth-first-search over all the tiles in the map, starting with the water tiles as roots and following edges to neighboring tiles. 对地图中的所有图块进行广度优先搜索,从水砖作为根,然后沿着边缘到相邻图块。 The level at which you you find a tile will be its distance from water. 找到瓷砖的水平将是它与水的距离。
See this answer for a nice way to keep track of the depth during BFS: 请参阅此答案,了解在BFS期间跟踪深度的好方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.