A millenia old chess riddle challenges a person to move a knight on an empty board such that every square on the board is visited exaclty once. For more on the history and significance of this problem, see the Wikipedia entry on the Knight's Tour.
Your task for this project is build a general solution finder for the problem that recursively searches for a successful tour on an n x m board, given as input the board size and the starting position of the knight.
The chess piece called, "the knight," moves in a unique L-shaped pattern. From a square near the middle of a chessboard, a single move consists of either two horizontal spaces and a single vertical space, or two vertical spaces and a horizontal space. Like all chess pieces, the knight cannot move out of the boundaries of the chessboard. Thus, a knight can have up to 8 moves from a given starting location, fewer if it is near the board edge.
A chessboard has 64 squares, organized into eight rows and eight columns, alternating in color. The color of a square is of no importance when moving a knight. For the purposes of this problem, we're going to generalize to allow rectangular chessboards of any dimension.
For a given board size, various clever algorithms exist for quickly detecting or constructing a successful knight's tour; none of these are necessary or appropriate for this project. Your task is to construct a simple, recursive, brute-force search of the possibilities. A modern computer can find solutions for an 8x8 board with some starting positions in under five seconds. This problem has exponential complexity as specified, so solutions for bigger boards can take a lot longer.
I recommend solving this problem with two classes, one to represent the chessboard, and one to encode the solution strategy. My ChessBoard class contains a private, two-dimensional array of integers to represent the board, and includes the following methods:
In the examples below, I use text in blue to distinguish the output of the program from the input I typed. This is for purpose of clarity only; your program will not print text in different colors.
Enter number of rows: 8