这是我在 python 中的代码,调用knightsTour(0,0,1,sol,xMove,yMove)
应该返回True
,但我得到False
. 我无法找到这个错误。
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)