Maze-solving algorithm

Intuitively, if one pulled and stretched out the paths in the maze in the proper way, the result could be made to resemble a tree.

[1] This simple method can be implemented by a very unintelligent robot or perhaps a mouse, because it does not require any memory.

If the maze is not simply-connected (i.e. if the start or endpoints are in the center of the structure surrounded by passage loops, or the pathways cross over and under each other and such parts of the solution path are surrounded by passage loops), this method will not necessarily reach the goal.

If however, the solver starts inside the maze, it might be on a section disjoint from the exit, and wall followers will continually go around their ring.

[4][5] The Pledge algorithm, designed to circumvent obstacles, requires an arbitrarily chosen direction to go toward, which will be preferential.

This allows the algorithm to avoid traps shaped like an upper case letter "G".

An algorithm that only keeps track of "current heading" leads into an infinite loop as it leaves the lower rightmost wall heading left and runs into the curved section on the left hand side again.

It follows the wall all the way around, finally leaving it heading left outside and just underneath the letter shape.

This algorithm allows a person with a compass to find their way from any point inside to an outer exit of any finite two-dimensional maze, regardless of the initial position of the solver.

However, this algorithm will not work in doing the reverse, namely finding the way from an entrance on the outside of a maze to some end goal within it.

Trémaux's algorithm, invented by Charles Pierre Trémaux,[6] is an efficient method to find the way out of a maze that requires drawing lines on the floor to mark a path, and is guaranteed to work for all mazes that have well-defined passages,[7] but it is not guaranteed to find the shortest route.

The algorithm works according to the following rules: The "turn around and return" rule effectively transforms any maze with loops into a simply connected one; whenever we find a path that would close a loop, we treat it as a dead end and return.

Without this rule, it is possible to cut off one's access to still-unexplored parts of a maze if, instead of turning back, we arbitrarily pick another entrance.

When you finally reach the solution, entrances marked exactly once will indicate a way back to the start.

Dead-end filling cannot accidentally "cut off" the start from the finish since each step of the process preserves the topology of the maze.

If the X and Y values are those of the end location, it will save all the previous instances of the method as the correct path.

Here is a sample code in Java: The maze-routing algorithm [11] is a low overhead method to find the way between any two locations of the maze.

The algorithm is initially proposed for chip multiprocessors (CMPs) domain and guarantees to work for any grid-based maze.

When a maze has multiple solutions, the solver may want to find the shortest path from start to finish.

The breadth-first search algorithm uses a queue to visit cells in increasing distance order from the start until the finish is reached.

The breadth-first search in its simplest form has its limitations, like finding the shortest path in weighted graphs.

Robot in a wooden maze
Traversal using right-hand rule ( animation )
Left: Left-turn solver trapped
Right: Pledge algorithm solution
Trémaux's algorithm. The large green dot shows the current position, the small blue dots show single marks on entrances, and the red crosses show double marks. Once the exit is found, the route is traced through the singly-marked entrances.

Note that two marks are placed simultaneously each time the green dot arrives at a junction. This is a quirk of the illustration; each mark should in actuality be placed whenever the green dot passes through the location of the mark.
A maze with many solutions and no dead-ends, where it may be useful to find the shortest path