0

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...')
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40

2 Answers2

0

I tried to run your code on my laptop (not really powerful configuration) for different chessboard dimensions:

  • 5x5: 2000 possible tours (0.11s)
  • 6x6: 7 millions possible tours (29s)
  • 8x8: 19,000,000 billions possible tours (expected execution time: 2.5 billion years)

Your code seems to work like a charm but there's simply too many possibilities to try out.

Hope it helped.

happyweary
  • 81
  • 2
0

I used modified version of you program at it worked for 8x8 , starting at position 0,4 (since I knew there is valid solution for that) and it took around 2 minute.

Following are changes I did

  1. Initialize Move No at 2, Since First move is done by setting first piece
  2. Increase MoveNo checking to 65
  3. I also modified sequence to check possible values staring at col+2, row+1 and doing clock wise
Vikrant Pawar
  • 789
  • 1
  • 6
  • 23