A chess knight must visit every square of an n × n board exactly once. Write KnightTour: read n and the starting row col (numbered from 0), and search for a tour by backtracking:
tour(r, c, move):
if move > n × n: success
for each free square (nr, nc) a knight's move away:
number it 'move' ← choose
if tour(nr, nc, move + 1): success ← explore
clear it ← un-choose
failure
Try the candidate squares in the order of fewest onward moves first (Warnsdorff's rule) — with ties broken by the order of the eight moves (−2,+1), (−1,+2), (+1,+2), (+2,+1), (+2,−1), (+1,−2), (−1,−2), (−2,−1). Print the board with each square's move number in a field of width 3, or a message when no tour exists, then the number of recursive calls.
For the input
5 0 0
the program prints
1 14 9 20 3
24 19 2 15 10
13 8 25 4 21
18 23 6 11 16
7 12 17 22 5
Recursive calls: 25