Professional Documents
Culture Documents
. Q . . . Q . .
Q . . . . . . Q
. . . Q Q . . .
. Q . . . . Q .
To find more solutions, we backtrack from this one: its easy to see there
are no more possibilities by considering columns 2,3,4. So we backtrack
to the first column and fill in so
. Q . .
. . . Q
Q . . .
. Q . .
This is a solution. Its also clear in this case that any backtracking to find
new solutions must go all the way back to the first column. Then we have
possibilities
. Q . .
. . . .
. . Q .
Q . . .
Now we must backtrack to the second column to find
. . . .
. Q . .
. . . .
Q . . .
This doesnt lead to a solution and there are no more ways to backtrack.
Thus we have found all the solutions to the Four Queens Problem.
2. Carrano, Chapter 6, Exercise 14: Execute the HPAir algorithm with the
map in Figure 6-17 for the following requests. Show the state of the stack
after each step.
In the solutions below, we use the HPAir algorithm by visiting cities from
a given one in alphabetical order.
num1 = 5
num2 = 1
num3 = 4
aQueue.enqueue(num2)
aQueue.enqueue(num3)
aQueue.dequeue()
aQueue.enqueue(num1 - num2)
aQueue.dequeue(num1)
aQueue.dequeue(num2)
cout << num2 << " " << num1 << " " << num3 << endl
Solution:
4 4 4