Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python“;骑士';“s之旅”;回溯代码不工作_Python_Numpy_Recursion - Fatal编程技术网

Python“;骑士';“s之旅”;回溯代码不工作

Python“;骑士';“s之旅”;回溯代码不工作,python,numpy,recursion,Python,Numpy,Recursion,在观看了Computerphile关于回溯的视频之后,我决定尝试为另一个问题实现这个算法。我的代码似乎一直工作到深度48,当它开始无休止地在深度46-53之间迭代时。这不是内存问题,对于6x6表来说,它同样会在深度20左右卡住 table = np.zeros((8, 8)) - 1 table[0, 0] = 0 knightDOWN = [1, 2, 2, 1, -1, -2, -2, -1] knightRIGHT = [2, 1, -1, -2, -2, -1, 1, 2] depth

在观看了Computerphile关于回溯的视频之后,我决定尝试为另一个问题实现这个算法。我的代码似乎一直工作到深度48,当它开始无休止地在深度46-53之间迭代时。这不是内存问题,对于6x6表来说,它同样会在深度20左右卡住

table = np.zeros((8, 8)) - 1
table[0, 0] = 0
knightDOWN = [1, 2, 2, 1, -1, -2, -2, -1]
knightRIGHT = [2, 1, -1, -2, -2, -1, 1, 2]
depth = 0
def correctmove(move, startROW, startCOL):
    newROW = startROW + knightDOWN[move]
    newCOL = startCOL + knightRIGHT[move]
    if newROW < 0 or newROW > 7:
        return False
    if newCOL < 0 or newCOL > 7:
        return False
    if table[newROW, newCOL] != -1:
        return False
    return True

def goBoard(startROW, startCOL, nummove):
    if depth == 64:
        print(table)
        exit()
    for x in range(0, 8):
        if correctmove(x, startROW, startCOL):
            print(table[startROW, startCOL])
            startROW += knightDOWN[x]
            startCOL += knightRIGHT[x]
            table[startROW, startCOL] = nummove
            nummove += 1
            goBoard(startROW, startCOL, nummove)
            table[startROW, startCOL] = -1
            startROW -= knightDOWN[x]
            startCOL -= knightRIGHT[x]
            nummove -= 1
    return
goBoard(0, 0, 0)
table=np.zero((8,8))-1
表[0,0]=0
奈特顿=[1,2,2,1,-1,-2,-2,-1]
骑士权=[2,1,-1,-2,-2,-1,1,2]
深度=0
def校正移动(移动、startROW、startCOL):
newROW=startROW+knightDOWN[移动]
newCOL=startCOL+knightRIGHT[移动]
如果新行<0或新行>7:
返回错误
如果newCOL<0或newCOL>7:
返回错误
如果表[newROW,newCOL]!=-1:
返回错误
返回真值
def goBoard(startROW、startCOL、nummove):
如果深度==64:
打印(表格)
退出()
对于范围(0,8)内的x:
如果正确移动(x、startROW、startCOL):
打印(表[startROW,startCOL])
startROW+=骑士时代[x]
startCOL+=骑士权利[x]
表[startROW,startCOL]=nummove
nummove+=1
goBoard(startROW、startCOL、nummove)
表[startROW,startCOL]=-1
startROW-=奈特顿[x]
startCOL-=骑士权利[x]
nummove-=1
返回
goBoard(0,0,0)
代码基本上应该检查它是否可以与骑士一起到达某个新的位置,直到它不能再向前移动为止,此时递归调用后的代码部分会再次重置它。在看到示例表后,它似乎正确地创建了前50次左右的尝试,但在这些尝试中被卡住了,并反复迭代。

这应该需要很长时间,而且要回溯很多次,因为您使用的是需要用尽很多可能性的蛮力算法

在您的代码中,我看不到哪里的
深度增加了,尽管它与
nummove
是多余的,后者应该从1开始,而不是0,因为您已经移动了0。我重新编写了您的代码以返回结果,而不是
print
exit
,删除了
numpy
,改为使用纯Python重新编写,并稍微简化了它--撤消的步骤更少:

KNIGHT_MOVES = [(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)]

table = [[-1] * 8 for _ in range(8)]

table[0][0] = 0

def correctmove(row, col):
    return 0 <= row <= 7 and 0 <= col <= 7 and table[row][col] == -1

def goBoard(startROW, startCOL, nummove):
    if nummove == 64:
        return table

    for down, right in KNIGHT_MOVES:
        row = startROW + down
        col = startCOL + right

        if correctmove(row, col):
            print(nummove)

            table[row][col] = nummove
            result = goBoard(row, col, nummove + 1)

            if result:
                return result

            table[row][col] = -1

    return None

print(*goBoard(0, 0, 1), sep='\n')
这应该需要很长的时间,而且要回溯很多,因为你使用的是一个暴力算法,需要用尽很多可能

在您的代码中,我看不到哪里的
深度增加了,尽管它与
nummove
是多余的,后者应该从1开始,而不是0,因为您已经移动了0。我重新编写了您的代码以返回结果,而不是
print
exit
,删除了
numpy
,改为使用纯Python重新编写,并稍微简化了它--撤消的步骤更少:

KNIGHT_MOVES = [(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)]

table = [[-1] * 8 for _ in range(8)]

table[0][0] = 0

def correctmove(row, col):
    return 0 <= row <= 7 and 0 <= col <= 7 and table[row][col] == -1

def goBoard(startROW, startCOL, nummove):
    if nummove == 64:
        return table

    for down, right in KNIGHT_MOVES:
        row = startROW + down
        col = startCOL + right

        if correctmove(row, col):
            print(nummove)

            table[row][col] = nummove
            result = goBoard(row, col, nummove + 1)

            if result:
                return result

            table[row][col] = -1

    return None

print(*goBoard(0, 0, 1), sep='\n')

不知道为什么它没有出现在我的收件箱中,但这是一个很好的答案。第二天,我用我的代码工作了一段时间,意识到你说的是真的——这是正确的(或者我做了一些修复,不确定),但最终我没有给它时间来获得解决方案。你的代码仍然教会了我很多东西,尤其是你是如何将正确的移动函数和主函数简化的。谢谢你,很抱歉回答晚了!不知道为什么它没有出现在我的收件箱中,但这是一个很好的答案。第二天,我用我的代码工作了一段时间,意识到你说的是真的——这是正确的(或者我做了一些修复,不确定),但最终我没有给它时间来获得解决方案。你的代码仍然教会了我很多东西,尤其是你是如何将正确的移动函数和主函数简化的。谢谢你,很抱歉回答晚了!