| | |
| Developing the Recursive Solution | page 4 of 7 |
In developing a recursive solution, consider the base cases first. What situation(s) cause the algorithm to stop?
- 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.
- Encountering a '
*' character means that we have run into a wall. The algorithm should stop.
- Arriving at a location which has a row or column value equal to 1 or
MAXROW or MAXCOL. We have found an exit point.
-
The general case of encountering a blank space requires the following steps:
- Change the character value from the blank space to the '
!' character.
- 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.
- 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.
-
When an exit point is found, we want to print only the successful trail of '!' marks that leads to an exit. As a result, it is necessary for each recursive call to work with a copy of the array. Why is this? If a reference to the original array is passed with each recursive call, the placement of '!' marks would be permanent in the data structure.
|