2

I have a program that creates a 2D array that looks similar to this:

X X X X X X X B X X
B X X X X X X X X X
B X X X X X X X X X
X X X X X X X B X X
X X X X X O B X X X
B X X X X X B X X X
X X X X X X X X X X
X X X X X X X X X X

X = empty space free to roam

B = blocked areas not free to roam

O = object being moved

And I would like help on figuring out how to find the shortest paths to any of the end points. Normally I would of used the Dijkstra's Algorithm however I have multiple points that are to be considered instead of having 1 point. The point of the object is getting to the edge and I would like to find the shortest path there.

avim
  • 979
  • 10
  • 25
  • [BFS](http://en.wikipedia.org/wiki/Breadth-first_search) will give you an answer. If you need something better [A*](http://en.wikipedia.org/wiki/A*_search_algorithm) with "distance for any border" as heuristic may be faster. – Alexei Levenkov Mar 05 '15 at 22:47

1 Answers1

5

Lee's algorithm: http://en.wikipedia.org/wiki/Lee_algorithm

It's essentially a BF search, here's an example: http://www.oop.rwth-aachen.de/documents/oop-2007/sss-oop-2007.pdf

To implement it effectively, check out: Change FloodFill-Algorithm to get Voronoi Territory for two data points? - when I say mark, you mark it with the number on the position you came from + 1.

For example, if you have this grid, where a * = obstacle and you can move up, down, left and right, and you start from S and must go to D, and 0 = free position:

S 0 0 0
* * 0 *
* 0 0 *
0 0 * *
* 0 0 D

You put S in your queue, then "expand" it:

S 1 0 0
* * 0 *
* 0 0 *
0 0 * *
* 0 0 D

Then expand all of its neighbours:

S 1 2 0
* * 0 *
* 0 0 *
0 0 * *
* 0 0 D

And all of those neighbours' neighbours:

S 1 2 3
* * 3 *
* 0 0 *
0 0 * *
* 0 0 D

And so on, in the end you'll get:

S 1 2 3
* * 3 *
* 5 4 *
7 6 * *
* 7 8 9

So the distance from S to D is 9. The running time is O(NM), where N = number of lines and M = number of columns. I think this is the easiest algorithm to implement on grids, and it's also very efficient in practice. It should be faster than a classical dijkstra, although dijkstra might win if you implement it using heaps.

Do this for any starting point and endpoint and chose the endpoint with the lowest integer in your case.

Community
  • 1
  • 1
Jason Roell
  • 6,679
  • 4
  • 21
  • 28
  • Nice explanation. Also matrix shown in the original sample (almost all cells are passable) will be more suitable for A* (which will be close to O(N+M) in this case). If there are significant number of non-passable cells (like shown in the answer) than BFS would be comparable. – Alexei Levenkov Mar 05 '15 at 23:01
  • Good observation @AlexeiLevenkov. I did not think about that. As usual I guess its one of those "it depends" answers :) – Jason Roell Mar 05 '15 at 23:04
  • 1
    Well every single time the object moves a new obstacle will be placed and it will need to recalculate the shortest path so the logic you have given me would work. I am going to implement it and if it works I will select this as the answer. Thanks. – I'Kei Amuquadoh Mar 05 '15 at 23:16
  • I tried all yesterday but it did not work. I think it may be my coding because the logic is sound. – I'Kei Amuquadoh Mar 06 '15 at 14:09