grid will be 12 x 12 and the starting location will be at 6,6. 1Doug Cooper and
Michael Clancy, Oh! Pascal! 2nd Edition, W. W. Norton (New York and London), ...
STUDENT OUTLINE Lesson 19: Recursive Array Programming INTRODUCTION:
Suppose you were trapped in a maze and you needed to find your way out. Your initial attempts are random and due to the same appearance of the white walls, you begin wasting time and energy trying the same routes over and over. After some thought and remembering a computer science algorithm, you take advantage of a tool in your pocket, a marking pen. You begin marking the walls with some notes and a time stamp. At each branch point, you mark the direction you are about to attempt with an arrow. If a dead-end is encountered just around the corner, you retreat to the branch point, mark “dead-end” for that direction, and try another direction. By marking which directions have failed, you avoid trying them again. This 2 dimensional problem and the required backtracking leads us to a recursive matrix problem! After defining the problem, an entire solution will be covered in the outline. Your programming exercise will also be a recursive matrix problem. The maze searching problem is translated from, Oh! Pascal!, 2nd edition.1 The key topics for this lesson are: A. B. C. D.
Defining the Maze Problem Developing the Recursive Solution The Solution to the Maze Problem Declaring enum Data Types
VOCABULARY:
BACKTRACKING
DISCUSSION:
A. Defining the Maze Problem 1. The maze will be defined as a 2-D grid of asterisks (marking walls) and blanks (marking potential paths). The size of the grid will be 12 x 12 and the starting location will be at 6,6.
1Doug Cooper and Michael Clancy, Oh! Pascal! 2nd Edition, W. W. Norton (New York and London), 1985, 362-
367. APCS - C++, Lesson 19
©1998, ICT
2. The data structure for this problem will be an apmatrix of char. The array will be declared as a 13 x 13 grid so that we can use locations 1..12 by 1..12. Row 0 and column 0 will not be used in the data structure. 3. The initial grid is stored as a text file and will be loaded into the array.
APCS - C++, Lesson 19
©1998, ICT
{Here is a copy of the mazeData.txt file}
*** ******** *** ***** *** ** **** *** * *** ***** ** *** ***** ** *** ***** ****** ***** **** **** * **** * ** **** * * ***** * ********** 4. A potential path out of the maze will be traced with a !, with moves limited to horizontal or vertical directions. 5. We will be at an exit point if we arrive at row 1 or MAXROW, or at column 1 or MAXCOL. 6. The solution is a backtracking algorithm involving a series of trial and error attempts. If a dead-end is hit, back up and try a different direction.
B. Developing the Recursive Solution 1. In developing a recursive solution, consider the base cases first. What situation(s) cause the algorithm to stop? a. We have arrived at a location which is off the data structure. It is important to catch this situation first to avoid an array indexing error. b. Encountering a '*' character means that we have run into a wall. The algorithm should stop. c. Arriving at a location which has a row or column value equal to 1 or MAXROW or MAXCOL. We have found an exit point. 2. The general case of encountering a blank space requires the following steps:
APCS - C++, Lesson 19
©1998, ICT
a. Change the character value from the blank space to the '!' character. b. Check to see if we are at an exit point. If we are, print the maze with a trail of '!' markers to the exit point. c. If we are not at an exit point, make 4 recursive calls to check all 4 directions, feeding the new coordinates of the 4 neighboring cells.
3. When a recursive call is made, a value parameter will be used in the formal parameter list of the function. Each recursive call will work with a copy of the array. Why is this? If a reference parameter was used the placement of ! marks would be permanent in the data structure. However, when an exit point is found we want to print only the successful trail of ! marks which leads to an exit.
C. The Solution to the Maze Problem // program threadTheMaze #include #include #include const int MAXROW = 12, MAXCOL = 12; const char BLANK = ' '; void loadTheMaze (apmatrix &maze); void printMaze (const apmatrix &maze); void traceMaze (apmatrix maze, int row, int col); void loadTheMaze (apmatrix &maze) /* When reading text from disk, you cannot use the extraction operator, >>, because it skips white space. You need to use the stream member function get, as used below. You will also need to get rid of the end-of-line marker, which is a single ASCII code 10 in C++ text file processing. */ { ifstream inFile ("Mac HD:ap96:mazedata.txt",ios::in); char c1; // to dump end-of-line markers for (int row=1; row