
Task
Problem: you have a standard 8x8 chessboard, empty but for a single knight on some square. Your task is to emit a series of legal knight moves that result in the knight visiting every square on the chessboard exactly once. Note that it is not a requirement that the tour be "closed"; that is, the knight need not end within a single move of its start position.

Input and output may be textual or graphical, according to the conventions of the programming environment. If textual, squares should be indicated in algebraic notation. The output should indicate the order in which the knight visits the squares, starting with the initial position. The form of the output may be a diagram of the board with the squares numbered according to visitation sequence, or a textual list of algebraic coordinates in order, or even an actual animation of the knight moving around the chessboard.

Input: starting square

Output: move sequence



Related tasks
 A* search algorithm
 N-queens problem
 Solve a Hidato puzzle
 Solve a Holy Knight's tour
 Solve a Hopido puzzle
 Solve a Numbrix puzzle
 Solve the no connection puzzle


int     Board(8+2+2, 8+2+2);            \board array with borders
int     LegalX, LegalY;                 \arrays of legal moves
def     IntSize=4;                      \number of bytes in an integer (4 or 2)
include c:\cxpl\codes;                  \intrinsic 'code' declarations
 
 
func    Try(I, X, Y);                   \Make a tentative move from X,Y
int     I, X, Y;
int     K, U, V;
[for K:= 0 to 8-1 do                    \for all possible moves...
    [U:= X + LegalX(K);                 \U and V are next square
     V:= Y + LegalY(K);
     if Board(U,V) = 0 then             \if square has not been visited then
        [Board(U,V):= I;                \ mark square with sequence number
        if I = 8*8 then return true;
        if Try(I+1, U, V) then return true      \led to solution?
        else Board(U,V):= 0;                    \no, undo tenative move
        ];
    ];
return false;
];      \Try
 
 
int  I, J;
[LegalX:= [2, 1, -1, -2, -2, -1, 1, 2];
 LegalY:= [1, 2, 2, 1, -1, -2, -2, -1];
 
for J:= 0 to 8+2+2-1 do                 \set up surrounding border for speed
    for I:= 0 to 8+2+2-1 do
        Board(I,J):= 1;
for J:= 0 to 8+2+2-1 do                 \reposition Board(0,0) to Board(2,2)
        Board(J):= Board(J) + 2*IntSize;
Board:= Board + 2*IntSize;
for J:= 0 to 8-1 do                     \empty board
    for I:= 0 to 8-1 do
        Board(I,J):= 0;
Text(0, "Starting square (1-8,1-8): ");  I:= IntIn(0)-1;  J:= IntIn(0)-1;
Board(I,J):= 1;                         \starting location is 0,0
 
if Try(2, I, J) then                    \try to find second square
        [for J:= 0 to 8-1 do            \draw board with knight's move sequence
            [for I:= 0 to 8-1 do
                [if Board(I,J) < 10 then ChOut(0, ^ );
                IntOut(0, Board(I,J));
                ChOut(0, ^ );
                ];
            CrLf(0);
            ];
        ]
else    Text(0, "No Solution.^M^J");
]