3

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)
false
  • 10,264
  • 13
  • 101
  • 209
Anuj Mittal
  • 83
  • 1
  • 9

1 Answers1

4

That one took me a while to spot. The error is that you modify x and y in each iteration of the for k in range(8) loop even when the new position is not safe or ultimately does not serve as the starting position for a successful knight's tour. x and y indicate your current starting position and should not be changed!

Your comment

#trying all moves from the current coordinate

shows what you wanted to do, but what you actually did is try a move, and if the new position is not save or does not serve as a starting position for a successful knight's tour, try another move from the new position instead of the current coordinate (i.e. the x and y values the function was called with).

Your code requires one, simple fix (note the comments):

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):
        new_x = x+xMove[k] # don't modify x!
        new_y = y+yMove[k] # don't modify y!

        if safe(new_x,new_y,sol): # call with candidate values
            sol[new_x][new_y] = move # mark candidate values on board
            if knightsTour(new_x,new_y,move+1,sol,xMove,yMove): # call with candidate values
                return True
            else :
                sol[new_x][new_y] = -1 # reset candidate values
    return False
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • That's a silly mistake i have done , thanks for pointing it out man. but it is not printing anything now neither True nor False. – Anuj Mittal Dec 30 '15 at 12:32
  • @AnujMittal When you include my modifications it prints `True`. Maybe you are not waiting long enough. The computation takes some time, on my machine about a minute. – timgeb Dec 30 '15 at 13:01
  • Yes i'm getting the ans now, i missed one thing previously. – Anuj Mittal Dec 30 '15 at 13:44
  • @AnujMittal ok, anything else I can help you with regarding this question, or is it solved? – timgeb Dec 30 '15 at 13:45
  • @timbeg Question is solved and i completely understood it now, thank you very much . – Anuj Mittal Dec 30 '15 at 15:37