Home | | Syllabus | | Assignments | | Lecture Notes
In class we learned about solving the eight queens problem using recursion with backtracking. The idea was that, starting from a particular configuration of queens, we would place a queen on a square, try to find a solution (recursively), then remove the queen from that square and try another square. In this problem you will use a similar strategy to solve acyclic mazes. From a given position on the maze, the function will move to an adjacent square, try to solve the maze, then move back to the original space. It will continue by moving to another adjacent square, until it finds a solution or until all adjacent squares have been tried without success. Like Theseus, who used a ball of string to keep track of his path as he moved through the labyrinth under King Minos' palace in Crete to slay the minotaur, this program will mark a path as it takes each successive move. When it backtracks, it will unmark the path (Think of Theseus winding up the ball of string as he backtracks out of the labyrinth).
First, copy the files for this assignment which can be found in:
The files that you need for this problem are:
The maze.h and maze.cc files have all the functions needed to create a maze and represent a path through the maze. You will only be writing functions in the file solveMaze.cc. You can compile this program with the command:
This will prompt for the width and height of a maze, build the maze and print it out. This version does not solve the maze. If you run solveMaze, it will look something like the following:
The maze is represented as an n x m grid (a 2-D array named maze_postion[n][m]). Each square in the grid can take on an integer value. The value of each square is initialized to zero except for the "end" of the maze, which is set to a value of 2. As the program moves through the maze, it should set the values of squares on the current path to 1 (this will allow the print-out to show the path when the program reaches the goal).
Walls of the maze are represented by two 2-D arrays: horizontal_wall[n][m+1], and vertical_wall[n+1][m]. The values of the elements of these arrays are false for those positions with no wall and true for positions where there is a wall. The positions of the walls relative to the grid are as follows:
Note that an increase in y indicates a move downward in the maze. The relationship of the walls and the gridlines is diagrammed below; the grid on the right shows the corresponding x and y coordinates for each grid position:
You must write a program to find a path from the start (at positions 0,0) to the end (marked/displayed with a $). As you move through the maze, you are not allowed to move through a wall. Thus, before you make any moves you must test whether there is a wall in the way.
Your task is to write the function solveMaze(). This will recursively solve the maze and print out the solution if it finds one. It will return a value of true if it finds the end of the maze, and false if it does not find the end (a grid space with value of 2). For the moment the end of the maze is always at the corner, but your program should work no matter where the end of the maze is placed. In order to write this function you should write the following auxilliary functions: isWallAbove(), isWallBelow(), isWalltoLeft() and isWalltoRight( ). Thus you need to fill in the following functions:
You will be able to make use of the following public maze functions:
Before you start working on this problem, you should write some short test functions to see what happens when you call these different maze functions. Familiarize yourself with the performance of these functions. You may also look at the implementation of these functions in the maze.cc file.
Start by implementing the auxilliary functions, (isWallAbove() etc.), since you will need these to implement solve(). The function, solve() should be a recursive function that uses backtracking to try out possible solutions. If no solution is found along a given path, the function will backtrack and try a different route. No path to a solution will ever cross itself for these acyclic mazes, so if the function ends up on a square that is already on the current path, it should return false and backtrack. Use the following strategy:
Important note: Your code for the function, solve(), should be short. My solution is under 30 lines. If you find yourself writing a very long function, you are on the wrong track.
Take a lot of time to think out this problem and make sure you understand the details before you attempt to write the solution.
Sample Output:
radius% ./solveMaze Do you want me to solve a maze (y/n)? y Enter number for width of maze: 5 Enter number for height of maze: 5 Maze to be solved: --------------- | | | --- --- | | | | ------ | | | | | --- | | | --- | | | | $ | --------------- --------------- |X X | | --- --- | |X | | ------ | |X | | | --- | |X X X X | --- | | | | $ | --------------- Do you want me to solve another maze (y/n)? y Enter number for width of maze: 15 Enter number for height of maze: 10 Maze to be solved: --------------------------------------------- | | | | | | | | ------ --------------- --- | | | | | | ------ --- --------- ------ --- | | | | | --- --------- ------------ --- | | | | | | | --------------------- --------- | | | | | | | ------ --------- --- ------ | | | | | | | --- ------------ --- | | | | | | | | --------------- ------------ | | | | | | | ------ ------------------------------------ | | ------------------ ------------------------ | $ | --------------------------------------------- --------------------------------------------- |X | | | | | | | ------ --------------- --- |X X X | | | | | ------ --- --------- ------ --- | X X X X X | | | | --- --------- ------------ --- | | | X X | | | | --------------------- --------- | | |X | | | | ------ --------- --- ------ | X X X X X X |X | | | | | --- ------------ --- | X X X X X |X X | | | | | | --------------- ------------ | X X X X | | | | | | ------ ------------------------------------ | X X X X X | ------------------ ------------------------ | X X X X X X X X $ | --------------------------------------------- Do you want me to solve another maze (y/n)? n radius%
Home | | Syllabus | | Assignments | | Lecture Notes
Computer Science 132--Data Structures
Last Modified: March 13, 2014
Page Expires: January 14, 2015