CS 112 Project 5
Amazing!
Due Friday, March 7th 2008

Solving Mazes

Mazes can be solved recursively, using a backtracking algorithm.  To find the ending location given a starting location:
  • If we are at the ending location already, return true
  • Mark the starting location as "visited"
  • For each reachable, unvisited location adjacent to the starting location
    • If we can reach the ending location from this adjacent position, return true
  • If we could not reach the solution from any reachable adjacent location, return false  

Assignment

For Project 4, you will create a program that reads in a square maze file from a file (which contains a starting and ending location), find a soluion (if there is one) recursively, and then print out the solution (if a solution exists).  The input files will contain the size of the maze, followed by an ASCII representation of the maze, where a wall is represented by an X, a space is represented by a ' ', the starting location is represeted by a 'S', and the ending location is represented by a 'E'.  For example a sample input file would be:

20
XXXXXXXXXXXXXXXXXXXX
XSX     X          X
X X  X  X X XXXXXX X
X    X  X X      X X
XXXXXX  X X XXXXXX X
X         X   X    X
X XXXXXXXXXXXXX  XXX
X   X              X
XXX X  XXXXXXXXXXXXX
X   X         X    X
X XXXXXXXXXX  X  XXX
X   X   XX    X  X X
X   X XXXX XXXX  X X
X     X        XXXXX
XXXXXXX XXXXXX XXXXX
X            X     X
XXXXXX XXXXXXXX XXXX
X  X X X  X   X  X X
X           XEX  X X
XXXXXXXXXXXXXXXXXXXX

Your program should print out if there is a solution, and if a solution exists, print the path of the solution using "*".  For instance, for the above input, you would print out:

Solution Exists
XXXXXXXXXXXXXXXXXXXX
XSX**** X**********X
X*X* X* X*X XXXXXX*X
X*** X* X*X      X*X
XXXXXX* X*X XXXXXX*X
X     ****X   X ***X
X XXXXXXXXXXXXX *XXX
X   X ***********  X
XXX X *XXXXXXXXXXXXX
X   X ******* X    X
X XXXXXXXXXX* X  XXX
X   X   XX*** X  X X
X   X XXXX*XXXX  X X
X     X****    XXXXX
XXXXXXX*XXXXXX XXXXX
X     **     X     X
XXXXXX*XXXXXXXX XXXX
X  X X*X**X***X  X X
X     ******XEX  X X
XXXXXXXXXXXXXXXXXXXX

Likewise, for the input:

10
XXXXXXXXXX
X        X
XXXXXXXX X
X    S   X
X XXXXXXXX
X X   X XX
X X X X  X
X X X XX X
X X XE   X
XXXXXXXXXX

Your program would output:

No solution exists

You do not need to do any error checking on the file -- you can assume that it is correct.  You should read the filename from the input parameter to the program

Implementation Hints

Program Decomposition

Your program should consist of a Maze class, which should have at least methods to read in a maze, solve a maze, and print out a solution.  You should probably have other helper methods as well, so that none of your methods are too long, or try to do too many things. 

Grading

Submission

All file(s) required for your project should be in the folder https://www.cs.usfca.edu/svn/<username>/cs112/Project5/

Maze Creation

The following applet will create random mazes for you

Prebuilt Mazes


Some prebuilt mazes, if the Applet is giving you trouble

Maze21
Maze31
Maze41
Maze51
Maze61
Maze71
Maze101
Maze201