Knights Tour solved using Python.
(And graphically with PyGame.)
This program was made as part of the very first homework of a course taken back in 2015. The way the problem was solved is using backtracking and recursion, applying the Wansdorff's rule.
The program won't need any arg variables, but it'll ask you for the dimensions of the board and the initial positions in x and y, in order to begin to compute a solution. The positions are integer values in [0,N-1]x[0,N-1]
.
Of course, some constraints apply to have a solution. If you want a solution, the N
from the input must be greater than 5. Also, for a graphical tour, N
must be less than 32.
To run the script, just open a terminal in the directory where the script and run it by doing:
python Knights.py
(You just have to be sure to have the knight.png
and chess.png
files.)
The program will return a numpy
matrix with the tour, and a list with the positions of the knight, which is used to create the graphical tour. An example of this can be seen here: