CSC 8310 Lisp Assignment: Maze Traversal
David Matuszek, Villanova University, Spring 2001

Your assignment is to write a Lisp program that finds a path through a maze.

Your program should take a description of a maze, a starting location, and an finish location, and should find a sequence (list) of locations forming a path from start to finish.

A maze will be described as a list of lists. Each sublist will have as its CAR a location in the maze, and as its CDR a list of the locations immediately adjacent to the first. Here's a (very small) example:

Maze: ( (1 2 4) (2 1 3) (3 2 6) (4 1 5) (5 4 8) (6 3 9) (7 8) (8 5 7 9) (9 6 8) )

So from location 1 you can go to either 2 or 4; from location 2 you can go to either 1 or 3; and so on.

An example problem would be: given this maze, find a path from 2 to 5. An example solution would be (2 3 6 9 8 5).

There is no guarantee that the maze will be rectangular. There is no guarantee on the number of locations that will be immediately adjacent to any given location. There is not even any guarantee that the maze will be planar. I will guarantee that there is at least one solution to any maze that I give you.

How to solve it

Here's one way of going about solving the maze problem; you can solve it some other way if you prefer. This explanation talks about the lists to be used, and says very little about the Lisp to do it. That part is up to you.

Start with a list (call it PATHS) containing one element: a list containing the starting location. For the above location, if the starting position is 2 and the finish position is 5, then PATHS should initially be ((2)). Note that we have not yet gotten to 5.

Add to PATHS a new sublist containing all the locations immediately adjacent to the locations in the first sublist. That is, the first sublist contains 2, so the new sublist should contain 1 and 3. PATHS then becomes ((1 3) (2)). We still have not gotten to 5.

Add to PATHS a new sublist containing all the locations immediately adjacent to the locations in the first sublist. That is, the first sublist is (1 3), so the new sublist should contain 4 and 2 (because these locations are adjacent to 1) and 2 and 6 (because these locations are adjacent to 3). PATHS then becomes ((4 2 2 6) (1 3) (2)). We still have not gotten to 5.

Without optimizations, PATHS can become extremely large, and your program may become too slow (or use too much memory) to solve even fairly small mazes. There are two optimizations that should be made at this point:

Add to PATHS a new sublist containing all the locations immediately adjacent to the locations in the first sublist. That is, if the first sublist is (4 6), we would get the new sublist (5 1 3 9) and optimize it to (5 9), so that PATHS becomes ((5 9) (4 6) (1 3) (2)). Finally, our new sublist contains the finish node 5, so we have found a path; we just have to extract it from this list of lists.

Starting with the list ((5 9) (4 6) (1 3) (2)), pull out the first sublist, take the 5 out of it, and put it into a second list (call it PATH). Then pull out the first remaining sublist, take from it any location adjacent to 5, and put it into the second list. Continuing in this fashion gives

PATHS PATH
((5 9) (4 6) (1 3) (2))   ()
((4 6) (1 3) (2)) (5)
((1 3) (2)) (4 5)
((2)) (1 4 5)
() (2 1 4 5)

General suggestions for programming in Lisp:

Due dates: Monday, April 9, and Wednesday, April 12.
Turn in a floppy containing: Source code and the results of test runs.