CS302 -- Lab 8

Solving Mazes


In this lab you will find paths through mazes. A maze is what you think it is. Our mazes will consist of a rectangular grid of r*c cells that can each have four walls. Here is an example of a 2x2 maze:

_ ___
| | |
|__ |
In this maze: Here's a 10x30 maze:
_ ___________________________________________________________
| ___ | __|   |   __| |   | __| __|__ |____ __|   __| __|   |
| | |       |__ |_| |__ | |   | |__ ___ _____   |__   |__ |_|
|   |_| | | | |___| | | |___| |__ |_| | | ___ |   |_| |__   |
| | __| |_|__ |   __|___|__ ________|   | __|_| |_| |__ __|_|
| |__ |___| | | |_| __|____ __|__ __| | | |   |_| __| ___ __|
|_| | |__ |   ___ |__ ___ __|__ |____ |_| | |_| |____ |___| |
|     | ____| __| | __|__   ___ | _____ |___| | |__   |__ __|
|_|_| |_| |   |__   |__ | |_|__ __|____   |__ | | __| |   | |
| | ____| | |__ | | | ___ __| | | |   | |   | ____| ____| | |
|_________|___|_|_|_|_|_____|_____|_|___|_|_|_____|___|____ |
A hole occurs where one of the four outer walls of the maze has an opening. Both of the above mazes have two holes, one at the upper left corner of the maze and one at the lower right corner of the maze. In general, mazes can have holes in any wall and can have multiple holes per wall.


Maze Files

We have defined a maze file format that is very simple. It consists of lines of text. These lines can fall into one of four categories: The following are example maze files: For a maze file to be valid, each internal wall of the maze must be specified in both cells to which it is incident. For example, the right wall of cell (0,0) in the 2x2 maze above is specified in both cell (0,0) and cell (0,1).

You can have maze files that don't satisfy the path criteria of mazes. For example, the following is a valid maze file, even though each cell is boxed in:

MAZE 2 X 2
CELL 0 0 L R T B
CELL 0 1 L R T B
CELL 1 0 L R T B
CELL 1 1 L R T B

Maztotxt

The program maztotxt.cpp prints out mazes from maze files. It takes a maze file on standard input and prints out a nice cheap ASCII representation of the maze:
UNIX> /home/parker/Courses/CS302-Fall06/Labs/Lab8/maztotxt < 02x02.maz
_ ___
| | |
|__ |

UNIX> /home/parker/courses/CS302-Fall06/Labs/Lab8/maztotxt < 05x04.maz
_________
  ___ | |
| | |__ |
|___|__ |
| |   __|
|___|__ |

UNIX>
Try it out.


More Background


First, take a look at maztoppm.cpp. This takes a maze file and turns it into a ppm file (see here for a description of ppm files). You can view ppm files with gimp (or perhaps you have another favorite display utility), but it is better to turn them into gif files using ppmtogif because ppm files get pretty big. A copy of ppmtogif is in the directory /usr/local/bin on the UTK machines and also in the /home/parker/Courses/CS302-Fall06/Labs/Lab8 directory. Copy it to your directory or make a soft link to it.

To reiterate, you should turn ppm files into gif files to conserve space. You probably don't want 20 megabytes of ppm files in your lab directories.

To display gif files, use

UNIX> display 02x02.gif
This can also be used remotely using gdk as per lab 3.

The syntax of maztoppm is:

maztoppm [cellwidth]
This takes a maze file on standard input, and produces a ppm file on standard output. The cell-width is how many pixels each cell of the maze is. The default is 10, and the minimum cell width is 3. Here are some examples: Try it out on your own mazes.

Fun, no?


Paths

Maztoppm allows you to specify a path through the maze. You do this with lines of the form
PATH row column
Basically, all this does is color that cell blue rather than white. For example, 05x04-s.maz has a solution of the maze 05x04.maz. Here is 05x04.gif and 05x04-s.gif.


MazeSolve.cpp

OK -- your job is to write MazeSolve.cpp. It reads a maze file on standard input and solves it, emitting a maze file with a path through the maze on standard output.

The maze must have at least 2 holes on its borders -- MazeSolve will choose a starting hole as follows:

  1. It will check the top wall of the maze, then the bottom wall, then the left wall, and finally the right wall.

  2. It will search the top and bottom walls from left to right and the left and right walls from top to bottom.

  3. It will select the first hole it finds.

Once your program finds a starting hole it should use depth-first search to try to find an ending hole. The ending hole can be any hole other than the starting hole.

At each cell in your depth-first search you will have a choice of four directions in which to proceed (actually only three choices because you entered the cell from one of the four directions). You should randomly select a direction to proceed by calling lrand48. Once you have chosen a direction you should proceed in a clockwise direction to explore the other directions. For example if you randomly choose to move right, then you should continue by moving down, left, and up. Notice that walls may prevent you from moving in some of these directions. Also the fact that you may have entered from one of these directions may prevent you from moving in that direction.

If there are no holes, MazeSolve should emit an error. If there is no path from the starting hole to a finishing hole, then MazeSolve will also emit an error.

Here are some examples:

Note, the two big mazes are the same maze with different starting holes.

OK -- you'll need to do this with depth first search, treating the maze as a graph. Moreover, you'll need to worry about cycles because some graphs, like 10x10-sparse.maz have lots of cycles, and you need to figure out how to deal with them.

That's it. You'll note, the provided MazeSolve binary chooses a random edge to go through first, which is why like 10x10-sparse-s.gif has a jagged looking path.

You can find a copy of MazeSolve as /home/parker/Courses/CS302-Fall06/Labs/Lab8/MazeSolve.

Enjoy.


A few more maze files

Your code should be able to handle mazes that don't have enough entry/exit holes, mazes that have more holes than needed, or mazes that don't have a path. Here are some example mazes for these situations:

What to submit

  1. makefile
  2. MazeSolve.cpp
  3. your header file (.h), if you made one
Do not submit maztoppm.cpp or any maz, ppm, or gif files.