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:
_ ___________________________________________________________ | ___ | __| | __| | | __| __|__ |____ __| __| __| | | | | |__ |_| |__ | | | |__ ___ _____ |__ |__ |_| | |_| | | | |___| | | |___| |__ |_| | | ___ | |_| |__ | | | __| |_|__ | __|___|__ ________| | __|_| |_| |__ __|_| | |__ |___| | | |_| __|____ __|__ __| | | | |_| __| ___ __| |_| | |__ | ___ |__ ___ __|__ |____ |_| | |_| |____ |___| | | | ____| __| | __|__ ___ | _____ |___| | |__ |__ __| |_|_| |_| | |__ |__ | |_|__ __|____ |__ | | __| | | | | | ____| | |__ | | | ___ __| | | | | | | ____| ____| | | |_________|___|_|_|_|_|_____|_____|_|___|_|_|_____|___|____ |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 rows X colsObviously, these define how big the maze is. You must have a size specification line before any cell specification lines.
CELL row column [LRTB]These specify the walls in the given cell. If a cell is not specified with a cell specification line, then it is assumed to have no walls.
MAZE 2 X 2 CELL 0 0 L R CELL 0 1 T L R CELL 1 0 B L CELL 1 1 R
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
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.
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.gifThis 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:
Fun, no?
PATH row columnBasically, 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.
The maze must have at least 2 holes on its borders -- MazeSolve will choose a starting hole as follows:
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:
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.