Here is my code in python, the call to knightsTour(0,0,1,sol,xMove,yMove)
should return True
but I am getting False
. I was not able to hunt the bug down.
def safe(x,y,sol):
return x >= 0 and x < 8 and y >= 0 and y < 8 and sol[x][y] == -1
def knightsTour(x,y,move,sol,xMove, yMove):
if move == 8*8 :
return True
#trying all moves from the current coordinate
for k in range(8):
x = x+xMove[k]
y = y+yMove[k]
if safe(x,y,sol):
sol[x][y] = move
if knightsTour(x,y,move+1,sol,xMove,yMove): #calling recursively
return True
else :
sol[x][y] = -1 #backtracking
return False
sol = [[-1 for i in range(8)]for j in range(8)]
sol[0][0] = 0
xMove = [2,1,-1,-2,-2,-1,1,2]
yMove = [1,2,2,1,-1,-2,-2,-1]
print knightsTour(0,0,1,sol,xMove,yMove)