I am attempting to develop an algorithm which finds the shortest path between two points on a square grid, whilst avoiding obstacles.
No diagonal movements are allowed, only up down left and right.
The algorithm is split into two parts:
-The labeling procedure,
-The actual path finding algorithm.
At the moment this is it:
----Labeling procedure----
1) Label the start box 0.
2) Label all boxes surrounding the start box that are not filled with an obstacle with 1,
If no such box exists and the end box is not yet labeled, then no path is available and stop.
3) Label all the boxes surrounding the boxes labeled 1 that are not filled with an obstacle (and not yet labeled) with 2.
If no such box exists and the end box is not yet labeled, then no path is available and stop.
3) Label all the boxes surrounding the boxes labeled 2 that are not filled with an obstacle (and not yet labeled) with 3.
If no such box exists and the end box is not yet labeled, then no path is available and stop.
4) Continue this procedure until all boxes that are not filled with an obstacle have a label.
----Path finding algorithm----
1) Start at box 0, let the current path be P.
2) If this is the end box then stop, otherwise go to step 3.
3) Let n be the label of the current box, and add the current box to P.
4) At random choose a box connected to the current box that is labeled with n+1 and is not redundant,
- If such a box exists, make that the current box, and go to step 2.
-If no such box exists then treat the current box as redundant, remove it from P and never return to it,
go back to the previous box and return to step 2.
----------------------------------------------------------------------------------------
Here are two animations showing the two algorithms in action...
Labeling procedure:

Path finding algorithm:

As you can see from the second animation, because the next box is chosen randomly (when there is a choice) it is possible to go the "wrong way" and have to back track.
Can you think of a "smart" way of picking which box to go to next so to minimize the amount of back tracking?
Edited by ga05as, 29 December 2011 - 10:17 PM.











