I wanted to solve the Knight's Tour and came up with the following program. It never gives a solution (even given 2 hours of time) even though it doesn't seem to be getting into an infinite loop anywhere.
I tried it with a 4x4 board (with one space filled) as I knew the solution to it and the code gave the right solution without any problems.
from __future__ import print_function
The recursive function which implements the backtracking algorithm:
def moveKnight(boolBoard, i, j, moveNo, previousMoves):
if moveNo == 63:
previousMoves.pop(0)
print('Success! The moves of the brave Knight are:')
for i in previousMoves:
print(i, end = '\t')
return True
possMoves = possibleMoves(boolBoard, i, j)
for nextMove in possMoves:
previousMoves.append(nextMove)
boolBoard[nextMove[0]][nextMove[1]] = False
if moveKnight(boolBoard, nextMove[0], nextMove[1], moveNo+1, previousMoves):
return True
previousMoves.remove(nextMove)
boolBoard[nextMove[0]][nextMove[1]] = True
return False
Function for finding out all possible moves of the knight given its position and the board:
def possibleMoves(boolBoard, posr, posc):
possMoves = [[]]
if posr + 2 < 8 and posc + 1 < 8:
if boolBoard[posr+2][posc+1]:
possMoves.append([posr+2, posc+1])
if posr+2 < 8 and posc - 1 >= 0:
if boolBoard[posr+2][posc-1]:
possMoves.append([posr+2, posc-1])
if posr+1 < 8 and posc + 2 < 8:
if boolBoard[posr+1][posc+2]:
possMoves.append([posr+1, posc+2])
if posr-1 >= 0 and posc+2 < 8:
if boolBoard[posr-1][posc+2]:
possMoves.append([posr-1, posc+2])
if posr-2>=0 and posc + 1 < 8:
if boolBoard[posr-2][posc+1]:
possMoves.append([posr-2, posc+1])
if posr - 2 >= 0 and posc - 1 >= 0:
if boolBoard[posr-2][posc-1]:
possMoves.append([posr-2, posc-1])
if posr - 1 >= 0 and posc - 2 >= 0:
if boolBoard[posr-1][posc-2]:
possMoves.append([posr-1, posc-2])
if posr + 1 < 8 and posc - 2 >= 0:
if boolBoard[posr+1][posc-2]:
possMoves.append([posr+1, posc-2])
possMoves.pop(0)
return possMoves
Main Function:
if __name__ == '__main__':
boolBoard = [[True]*8]*8
for i in range(0, 8):
boolBoard[i] = [True, True, True, True, True, True, True, True]
previousMoves = [[]]
boolBoard[0][0] = False
if not moveKnight(boolBoard, 0, 0, 0, previousMoves):
print('Not Possible Bro...')