maze traversal - Exercise 7.22

logistic_guy

Senior Member
Joined
Apr 17, 2024
Messages
2,213
The following grid is a double-subscripted array representation of a maze.
maze.png
The \(\displaystyle \#\) symbols represent the walls of the maze, and the periods \(\displaystyle (.)\) represent squares in the possible paths through the maze. There’s a simple algorithm for walking through a maze that guarantees finding the exit (assuming there’s an exit). If there’s not an exit, you’ll arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the maze turns to the right, you follow the wall to the right. As long as you do not remove your hand from the wall, eventually you’ll arrive at the exit of the maze. There may be a shorter path than the one you have taken, but you’re guaranteed to get out of the maze.

Write recursive function \(\displaystyle \text{mazeTraverse}\) to walk through the maze. The function should receive as arguments a \(\displaystyle 12\)-by-\(\displaystyle 12\) character array representing the maze and the starting location of the maze. As \(\displaystyle \text{mazeTraverse}\) attempts to locate the exit from the maze, it should place the character \(\displaystyle \text{X}\) in each square in the path. The function should display the maze after each move so the user can watch as the maze is solved.
 
Top