Course Links

Exercises

Resources

External

Recursion Notes

Note that the recursive process as described has a nice geometric interpretation, as though the computer is simulating someone actually moving around inside the maze. The recursive call corresponds to exploration: as long as there is an unexplored path, it chooses a direction and follows it. Returning false from a recursive call corresponds to backtracking: when the computer reaches a dead end, it retraces its path until it finds a new unexplored area. Likewise, when it reaches the finish, it carries the news back to the start by retracing its steps while returning true. At any point in the maze, the call stack contains a record of the path taken from the start to that point, like a trail of breadcrumbs left behind. (Note: an alternate recursive formulation would make a new recursive call for every move, whether exploration or backtracking. While perfectly valid, this approach has the disadvantage of growing the call stack excessively. On the other hand, it would not require a globally updated maze state.)

The program ends when the goal is found, or if the computer is forced to backtrack all the way to its starting point, and has no more options. Note that in the first case, when the goal is found, the call stack contains a record of the exact sequence of moves that led there from the starting point! Your program could use this fact to output the full sequence of steps that solves the maze.

Note: It is easy to prove that the recursive process described above will always terminate eventually. As long as the maze is finite, each recursive call explores an unmarked square, and a square is marked as visited at each recursive call, thus there cannot be more recursive calls made than there are open locations in the maze.