Knight's Tour

Chapter: Knight's Tour

Recall how a knight moves on the chessboard. It can move two squares horizontally followed by one square vertically; or two vertically followed by one horizontally. An interesting question is:

Can a knight move around the board, visiting each square once and once only?

Here is my code for the applet below:


            Your browser is ignoring the <APPLET> tag!      

The applet searches for every knight's tour beginning at the top left corner. When it finds a solution it beeps, waits ten seconds for you to look at it, beeps again, and proceeds to find the next solution.

Look at my code and notice that I am no longer maintaining an explicit stack of partial solutions. Rather, I am extending my partial solutions by calling a recursive procedure findall. As we know, procedure calling on modern computer architectures is stack-based. And that is how I am implicitly maintaining a stack of partial solutions.

It turns out that some squares are "harder" than others in that there are fewer legal knight's moves into and out of them. This table indicates how many legal knight's moves are possible from each square (so the "harder" squares have the smallest numbers):
2 3 4 4 4 4 3 2
3 4 6 6 6 6 4 3
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
3 4 6 6 6 6 4 3
2 3 4 4 4 4 3 2

I have a hunch that if I try to get into and out of the hardest squares early on in my knight's tours, then I will more quickly find a solution.


Exercise 8

Check out my hunch!

Modify my code or write your own from scratch so the knight's tour will take into account how hard a given node is. Specifically:

When you improve the efficiency of a search by adding rules such as this, we say you are applying heuristics. Heuristics are "hunches" that you incorporate into your code to guide an otherwise blind search. Heuristics have contributed significantly to the field of Artificial Intelligence. Read all about it in Judea Pearl's classic Heuristics: Intelligent Search Strategies for Computer Problem Solving (ISBN 0201 -05594-5).

Perform the experiment, and any further experiments you think of. Report your results and conclusions, preferably in a nice beautiful LaTeX-prepared .pdf document. Send it to me, not the lab instructors. You can score 10 more exam points for a good solution to this investigation. Plus a further 2 exam points if it's beatifully presented using LaTeX.


A re-entrant knight's tour is a tour where the 64th square is within a knight's move of square number 1, so that it would be possible to close the tour by ending up at the starting point. This is a special case of a Hamiltonian circuit, as it is known in Graph Theory.

One last optional exercise: It will combine the beginning and the end of this lab -- magic squares and re-entrant knight's tours.


Exercise 9

Write a program to find a re-entrant knight's tour so that the numbers on the 8*8 chessboard form a magic square! Be warned: If you start on this it may haunt you for much of your life!


rhyspj@gwu.edu