Professional Documents
Culture Documents
Maze Problem
Maze Problem
The rat-in-a-maze experiment is a classical one from experimental psychology. A rat (or
mouse) is placed through the door of a large box without a top. Walls are set up so that movements
in most directions are obstructed. The rat is carefully observed by several scientists as it makes its
way through the maze until it eventually reaches the other exit. There is only one way out, but at
the end is a nice hunk of cheese. The idea is to run the experiment repeatedly until the rat will zip
through the maze without taking a single false path. The trials yield his learning curve.
We can write a computer program for getting through a maze and it will probably not be any
smarter than the rat on its first try through. It may take many false paths before finding the right
one. But the computer can remember the correct path far better than the rat. On its second try it
should be able to go right to the end with no false paths taken, so there is no sense re-running the
program. Why don't you sit down and try to write this program yourself before you read on and
look at our solution. Keep track of how many times you have to go back and correct something.
This may give you an idea of your own learning curve as we re-run the experiment throughout the
book.
Let us represent the maze by a two dimensional array, MAZE(1:m,1:n), where a value of 1 implies a
blocked path, while a 0 means one can walk right on through. We assume that the rat starts at
MAZE(1,1) and the exit is at MAZE(m,n).
With the maze represented as a two dimensional array, the location of the rat in the maze can at
any time be described by the row, i, and column, j of its position. Now let us consider the possible
moves the rat can make at some point (i,j) in the maze. Figure 3.6 shows the possible moves from
any point (i,j). The position (i,j) is marked by anX. If all the surrounding squares have a 0 then the
rat can choose any of these eight squares as its next position. We call these eight directions by the
names of the points on a compass north, northeast, east, southeast, south, southwest, west, and
northwest, or N, NE, E, SE, S, SW, W, NW.
We must be careful here because not every position has eight neighbors. If (i,j) is on a border
where either i = 1 or m, or j = 1 orn, then less than eight and possibly only three neighbors exist.
To avoid checking for these border conditions we can surround the maze by a border of ones. The
array will therefore be declared as MAZE(0:m + 1,0:n + 1).
Another device which will simplify the problem is to predefine the possible directions to move in a
table, MOVE(1:8.1:2), which has the values
MOVE
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
1
--1
-1
0
1
1
1
0
-1
2
-0
1
1
1
0
-1
-1
-1
north
northeast
east
southeast
south
southwest
west
northwest
By equating the compass names with the numbers 1,2, ...,8 we make it easy to move in any
direction. If we are at position (i,j) in the maze and we want to find the position (g,h) which is
southwest of i,j, then we set
g i + MOVE(6,1);
h j + MOVE(6,2)