Question
Do you know how to nd your way through a maze? After you write this program, you will never be lost again! Assume that a
Do you know how to nd your way through a maze? After you write this program, you will never be lost again!
Assume that a maze is a rectangular array of squares, some of which are blocked to represent walls. The
maze has one entrance and one exit. For example, if x s represent the walls, a maze could appear as follows:
xxxxxxxxxxxxxxxxxx x
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxx o xxxxxxx
A creature, indicated in the previous diagram by o , sits just inside the maze at the entrance (bottom row).
Assume that the creature can move in only four directions: north, south, east, and west. In the diagram, north is
up, south is down, east is to the right, and west is to the left. The problem is to move the creature through the
maze from the entrance to the exit (top row), if possible. As the creature moves, it should mark its path. At the
conclusion of the trip through the maze, you should see both the correct path and incorrect attempts. Write a
program to solve this problem.
Squares in the maze have one of several states: CLEAR (the square is clear), WALL (the square is blocked
and represents part of the wall), PATH (the square lies on the path to the exit), and VISITED (the square was
visited, but going that way led to an impasse).
This problem uses two ADTs that must interact. The ADT creature represents the creatures current position
and contains operations that move the creature. The creature should be able to move north, south, east, and west
one square at a time. It should also be able to report its position and mark its trail.
The ADT maze represents the maze itself, which is a two-dimensional rectangular arrangement of squares.
You could number the rows of squares from the top beginning with zero, and number the columns of squares
from the left beginning with zero. You could then use a row number and a column number to uniquely identify
any square within the maze. The ADT clearly needs a data structure to represent the maze. It also needs such data
as the height and width of the maze given in numbers of squares; the length of a side of a square, and the row and
column coordinates of both the entrance to and the exit from the maze.
The ADT maze should also contain, for example, operations that create a specic maze given descriptive
data that we will detail to display a maze, determine whether a particular square is part of the wall, determine
whether a particular square is part of the path, and so on.
The search algorithm and its supporting functions are outside both of the ADTs creature and maze. Thus,
the maze and the creature will be arguments that you must pass to these functions. If you are at the mazes
entrance, you can systematically nd your way out of the maze by using the following search algorithm. This
involves backtrackingthat is, retracing your steps when you reach an impasse.
Step 1. First check whether you are at the exit. If you are, youre done (a very simple maze); if you are not,
go to step 2.
Step 2. Try to move to the square directly to the north by calling the function goNorth (step 3).
Step 3. If goNorth was successful, you are done. If it was unsuccessful, try to move to the square directly
to the west by calling the function goWest (step 4).
Step 4. If goWest was successful, you are done. If it was unsuccessful, try to move to the square directly to
the south by calling the function goSouth (step 5).
Step 5. If goSouth was successful, you are done. If it was unsuccessful, try to move to the square directly
to the east by calling the function goEast (step 6).
Step 6. If goEast was successful, you are done. If it was unsuccessful, you are still done, because no path
exists from the entrance to the exit.
The function goNorth will examine all the paths that start at the square to the north of the present square
as follows. If the square directly to the north is clear, is inside the maze, and has not been visited before,
move into this square and mark it as part of the path. (Note that you are moving from the south.) Check
whether you are at the exit. If you are, youre done. Otherwise, try to nd a path to the exit from here by try-
ing all paths leaving this square except the one going south (going south would put you back in the square
from which you just came) as follows. Call goNorth; if it is not successful, call goWest and, if it is not suc-
cessful, call goEast. If goEast is not successful, mark this square as visited, move back into the square to the
south, and return.
The following pseudocode describes the goNorth algorithm:
goNorth(maze, creature)
if (the square to the north is clear, inside the maze, and unvisited )
{
Move to the north
Mark the square as part of the path
if (at exit)
success = true
else
{
success = goNorth(maze, creature)
if (!success)
{
success = goWest(maze, creature)
if (!success)
{
success = goEast(maze, creature)
if (!success)
{
Mark square visited
Backtrack south
}
}
}
}
}
else
success = false
return success
The goWest function will examine all the paths that start at the square to the west of the present square as
follows. If the square directly to the west is clear, is inside the maze, and has not been visited before, move into
this square and mark it as part of the path. (Note that you are moving from the east.) Check whether you are at the
exit. If you are, youre done. Otherwise, try to nd a path to the exit from here by trying all paths leaving this
square except the one going east (this would put you back in the square from which you just came) as follows.
Call goNorth ; if it is not successful, call goWest ; and if it is not successful, call goSouth . If goSouth is not suc-
cessful, mark this square as visited, move back into the square to the east, and return. The functions goEast and
goSouth are analogous to goWest and goNorth .
The input data to represent a maze is simple. For example, the previously given maze is represented by the
following lines of input, which can be in a text le:
20 7 width and height of the maze in squares
0 18 row and column coordinate of maze exit
6 12 row and column coordinate of maze entrance
xxxxxxxxxxxxxxxxxx x
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxx xxxxxxx
After the rst three lines of numeric data in the le, each of the next lines corresponds to a row in the maze, and
each character in a line corresponds to a column in the maze. An x indicates a blocked square (part of the wall),
and a blank indicates a clear square. This notation is convenient, because you can see what the maze looks like as
you design it.
C++
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started