Table of contents |

2 A heuristic 3 x2 && y1 + incy[i] 4 External links |

In the following we will see a method that is easy to learn and can be used to easily construct a knight's tour in less than 5 minutes. The tours so constructed will have the additional property that it is possible for the knight to step from the final square to the initial square, thus forming a closed path or a **cycle**.

Broadly, our strategy is to stay near the edges of the board as much as possible, so that towards the end, the squares that are remaining form a well-connected subgraph at the center of the board, enabling us to complete the last few moves by trial and error.

- Start from one of the edge squares (but
*not*one of the corner squares). - Keep close to the edges of the board, and avoid the 16 central squares unless there is no alternative (e.g. move 13 of figure 1).
- If you are one move away from a corner square, you must necessarily move to the corner square (See figure 1).
- Follow these rules until all edge squares and all squares bordering an edge square have been visited. You should be in a position similar to figure 2.

- 2a) Try to complete the tour by inspection. This is often not possible. If that is the case, read on.
- 2b)
*Arbitrarily*extend the path to get a cycle (i.e. without visiting all the remaining squares). In figure 3, the path has been extended to a cycle of length 54. - 2c) Find pairs of squares along the tour such that
- they are one move away from each other.
- each is one move away from some unvisited square.

- 2d) Use such pairs to complete the tour by inserting new squares between the pair of squares. In figure 3, this has been done using the pairs (5,6) and (8,9).
- 2e) Renumber the squares from 1 to 64 (see figure 4).

** Figure 3. ** The sequence 5->6 has been extended to 5->5a->5b->5c->5d->6 and 8->9 has been extended to 8->8a->8b->8c->8d->8e->8f->9 to complete the tour.

** Figure 4. ** Completed and properly numbered tour.

One popular heuristic for solving the knight's tour problem is the **Warnsdorff rule**. According to this rule, at each step the knight must move to that square which has the lowest degree (the degree of a square is the number of squares to which the knight can move from that square). The rationale for this is that towards the end, we will be left with squares having a high degree, and so there is a smaller chance of arriving at a square from which there is no move. The Warnsdorff rule is particularly suitable for writing computer programs to solve the knight's tour. Below is an example C program which uses this heuristic.

**Example C program for solving the knight's tour using the Warnsdorff rule**

#includeNote that the above program is not restricted to 8x8 chessboards; the parameters BWID and BHEIT control the width and the height respectively.#include /* The dimensions of the chess board */ #define BWID 8 #define BHEIT 8 /* The moves of the knight */ #define JWID 1 #define JHEIT 2 /* Macros for convenience */ #define isinboard(board,x,y) ((x)>=0 && (y)>=0 && (x) ## x2 && y1 + incy[i]

y2) return 1; return 0; }/* Generate a path with a random starting square and each square * chosen according to the smallest degree rule. If a hamiltonian * cycle results, print the solution and return 1. Else return 0. */ int gen_sol() { /* board[y * BWID + x] represents the move number in which the square (x, y) was visited. It is -1 if the square has not been visited so far */ int board[BWID*BHEIT]; /* x and y represent the coordinates of the square being currently visited */ int i, x, y, startx, starty; for(i=0; i

One solution obtained using the above program is shown below.

** Figure 5. ** Computer solution using the Warnsdorff heuristic.