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
- You will likely need to maintain 2 different 2D arrays: an
array of characters to hold the maze, and an array of booleans to
denote when a maze location has already been visited
- The easiest way to print out the path is to change the maze
itself, by adding '*' characters when you know a path is correct
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
- Following coding standards: Code is appropriately
commented, variables are properly named, code is formatted correctly,
no single function tries to do too much 15 points
- Function decomposition is important! Each function should
be short, and only do one thing. If a function is getting too
long, break it into smaller helper functions.
- Read maze in / write maze our correctly: 15 points
- Determines if there is a solution correctly: 35 points
- Prints out solution patj properly: 35 points
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