Imagine you are inside a maze and have to find your way out. What strategy can you use? One strategy you can use is the following
put your right hand on the wall
while you are still inside the maze do
move forward while keeping your hand on the wall
Unfortunately this strategy works correctly on some mazes only. E.g., it fails on this simple maze:
A maze can be represented textually. For example, the previous maze is represented like this
+-+-------+---+
| | | |
| + +---+ +-+ |
| | | | |
| + | + | + + |
| | | | | | |
| +-+-+ | +-+ +
| S| |
| + +-+ +-+ +-+
| | | |
+-+---+-------+
Your starting location is at S. The symbol +
indicates a corner, and the symbols |
or -
indicate a vertical or a horizontal wall, respectively.
Consider the following maze:
+-+-------+---+
| | | |
| + +---+ +-+ |
| | | | |
| + | + +-+ + |
| | | | | | |
| +-+-+ | +-+ |
| | S| | |
| | +-+ +-+ | |
| | | | |
| +-+ +---+ + |
| | | |
| + +---+ +---+
| | | |
+-+---+ +-----+
There are three choices for your initial move: go north, go west, or go south. It turns out that the first two choices lead to dead ends and going south is the only correct choice. You can see this because you have the whole picture of the maze and can imagine following some possibilities to the very end. However, the algorithm can make use of local information only.
x
characters.The pseudocode for the algorithm is as follows:
solveMaze(start) {
if the current square (start) is outside the maze then return success
if the current square (start) is marked or is a wall then return failure
mark this square (start)
for each neighbor of start square do
if solveMaze(neighbor) then return success
unmark this square (start)
return failure
}
The function solveMaze
should be called with start
set to the position of S
in the maze.
If the solveMaze
function works correctly, we should get this output:
+-+-------+---+
| |xxxxxxx| |
| +x+---+x+-+ |
|xxx| |xxx| |
|x+ | + +-+x+ |
|x| | | | |xxx|
|x+-+-+ | +-+x|
|x| x| |x|
|x| +-+x+-+ |x|
|x| |xxxxx|x|
|x+-+ +---+x+x|
|xxx| |xxx|
| +x+---+ +---+
| |xxxxx| |
+-+---+x+-----+
We'll store the answer in an array pi
, where for each row i
, the value pi[i]
is the column in which the queen in row i
should be placed. E.g., the solution to the 4-queens puzzle on the left is represented by the array pi
on the right.
column
0 1 2 3
+---+---+---+---+
0 | | Q | | |
+---+---+---+---+ 0 1 2 3
1 | | | | Q | +-+-+-+-+
row +---+---+---+---+ pi |1|3|0|2|
2 | Q | | | | +-+-+-+-+
+---+---+---+---+
3 | | | Q | |
+---+---+---+---+
n
queens have already been placed successfully on the board. The algorithm declares success, prints a solution, and stops.i
, it recursively calls itself on row i+1
. If no compatible placement positions in row i
leads to a success, the algorithm returns failure and backtracks to row i-1
to try a next compatible placement position in row i-1
.The pseudocode for the algorithm is as follows:
solveQueen(i) {
if row i = n then {
print solution in pi
return success
} else {
for each column j from 0 to n-1 do {
if the queen at square (i, j) is compatible with all
queens at squares (0, pi[0]), ..., (i-1, pi[i-1]) then {
set pi[i] := j
if solveQueen(i+1) = success then return success
}
}
return failure
}
}
The function solveQueen
should be called with i
set to 0.