Friday, April 5, 2013

If you get lost...

As promised, I have been looking for maze algorithms. I found two that are of particular interest because we can use them in real life size mazes. One is the left (or right) hand rule. It is technically called the...

Wall Follower algorithm

Start at the entrance and continue into the maze. Whenever you reach a junction always turn right (or left). This is equivalent to a human solving a maze by putting their hand on the right (or left) wall and leaving it there as they walk through the maze. If you like you can mark what cells you've visited, and what cells you've visited twice, you can retrace the solution by following those cells visited once. This method won't find the shortest solution. It is also ineffective when the goal is in the center of the Maze inside a closed circuit. In this case, you will end up going around the center and returning to the starting position. This method can also be used for 3d mazes by projecting the 3D passages onto the 2D plane. For example you would pretend up passages actually lead northwest and down lead southeast, and then apply normal wall following rules. The example starts out at the entrance to the maze following the left hand rule. As we go through the maze we make circles or loops and end up on a path that we were at before. The gray paths are these loops. We still follow the left hand rule, minding the areas that we have marked as loops and we eventually make it to end of the maze. As shown, this is not the shortest solution. It however, works on mazes with loops unlike the dead end filler method. 


The second, is the Pledge algorithm

This is a modified version of wall following that is able to jump between islands. It is used to solve mazes that wall following can't. It's a guaranteed way to reach an exit on the outer edge of any 2D Maze from any point in the middle. However it is not able to do the reverse, or find a solution within the maze. We start by picking a direction and continuing to move in that direction whenever possible. When a wall is hit, we start wall following until our chosen direction is available again. When wall following, we count the number of turns we make; a left turn is -1 and a right turn is 1. We stop wall following and continue in our chosen direction when the total number of turns we have made is equal to 0. Even if we have turned around 360 degrees or more, we keep wall following until we are untwisted and can continue in our direction. The counting ensures we eventually reach the far side of the island we are currently on, and jump to the next island in our chosen direction, where we will keep island hopping in that direction until we hit the edge of the maze. From here, wall following takes us to the exit. The pledge algorithm may make us visit a passage or the start more than once, although each time will always be with different turn totals. Thus this method can be extremely lengthy. As shown in the example, the solve continually tries to get to the right side of the maze while using the left hand rule.


As it turns out, these two examples have the exact same solution for the maze. I did not realize this until I posted the picture.

No comments:

Post a Comment