Assuming the map is rectangular, you can loop over all border points, and start a flood fill to find the most distant point from the starting point:
bestSolution = { start: (0,0), end: (0,0), distance: 0 };
for each point p on the border
flood-fill all points in the map to find the most distant point
if newDistance > bestSolution.distance
bestSolution = { p, distantP, newDistance }
end if
end loop
I guess this would be in O(n^2)
. If I am not mistaken, it's (L+W) * 2 * (L*W) * 4
, where L
is the length and W
is the width of the map, (L+W) * 2
represents the number of border points over the perimeter, (L*W)
is the number of points, and 4
is the assumption that flood-fill would access a point a maximum of 4 times (from all directions). Since n
is equivalent to the number of points, this is equivalent to (L + W) * 8 * n
, which should be better than O(n
2)
. (If the map is square, the order would be O(16n
1.5)
.)
Update: as per the comments, since the map is more of a maze (than one with simple obstacles as I was thinking initially), you could make the same logic above, but checking all points in the map (as opposed to points on the border only). This should be in order of O(4n
2)
, which is still better than both F-W and Dijkstra's.
Note: Flood filling is more suitable for this problem, since all vertices are directly connected through only 4 borders. A breadth first traversal of the map can yield results relatively quickly (in just O(n)
). I am assuming that each point may be checked in the flood fill from each of its 4 neighbors, thus the coefficient in the formulas above.
Update 2: I am thankful for all the positive feedback I have received regarding this algorithm. Special thanks to @Georg for his review.
P.S. Any comments or corrections are welcome.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…