骑士之旅迭代死胡同

时间:2019-02-21 20:39:50

标签: python recursion iteration backtracking

我正在尝试使用迭代方法实现骑士之旅。我已经使用递归编写了一个程序,并且可以正常工作,现在代替递归,而是使用带有堆栈的迭代方法来实现骑士之旅,我编写了以下代码。 在这里,当我走到尽头时,我无法回溯,请您检查我的代码并给我一些解决方案。

class knightTour:
        def __init__(self,size):
            self.size = size
            self.board = [[0 for x in range(self.size)] for y in range(self.size)]
    def is_valid_move(self,row ,col):
        if (row<0 or row>self.size-1 or col<0 or col>self.size-1 or self.board[row][col]!=0):
            return False
        return True

    def display(self):
        print
        for x in range(self.size):
            for y in range(self.size):
                print self.board[x][y],
            print
        print

    def solve(self,row,col,count,stack):
        self.board[row][col] = count
        count +=1
        stack.push(row)
        stack.push(col)

        while count < self.size*self.size:

            if (self.is_valid_move(row+2,col+1)):
                row+=2
                col+=1
                self.board[row][col] = count
                count+=1
                stack.push(row)
                stack.push(col)

            elif (self.is_valid_move(row+1,col+2)):
                row+=1
                col+=2
                self.board[row][col] = count
                count+=1
                stack.push(row)
                stack.push(col)

            elif (self.is_valid_move(row-1,col+2)):
                row-=1
                col+=2
                self.board[row][col] = count
                count+=1
                stack.push(row)
                stack.push(col)

            elif (self.is_valid_move(row-2,col+1)):
                row-=2
                col+=1
                self.board[row][col] = count
                count+=1
                stack.push(row)
                stack.push(col)

            elif (self.is_valid_move(row-2,col-1)):
                row-=2
                col-=1
                self.board[row][col] = count
                count+=1
                stack.push(row)
                stack.push(col)

            elif (self.is_valid_move(row-1,col-2)):
                row-=1
                col-=2
                self.board[row][col] = count
                count+=1
                stack.push(row)
                stack.push(col)

            elif (self.is_valid_move(row+1,col-2)):
                row+=1
                col-=2
                self.board[row][col] = count
                count+=1
                stack.push(row)
                stack.push(col)

            elif (self.is_valid_move(row+2,col-1)):
                row+=2
                col-=1
                self.board[row][col] = count
                count+=1
                stack.push(row)
                stack.push(col)

            else:
                 # " Backtrack   " // how to do backtracking here ?
                self.board[row][col] = 0
                count-=1
                stack.pop()
                stack.pop()
                col = stack.pop()
                row = stack.pop()
                stack.push(row)
                stack.push(col)
stack = stack()
d = knightTour(8)
d.solve(0,0,1,stack)
d.display()

1 个答案:

答案 0 :(得分:0)

def solve(self,row,col,count,stack):
    self.board[row][col] = count
    count +=1
    stack.append(row)
    stack.append(col)
    org=0
    origin=[]
    while count < self.size*self.size:

        if (self.is_valid_move(row+2,col+1) and org<1):
            row+=2
            col+=1
            self.board[row][col] = count
            count+=1
            stack.append(row)
            stack.append(col)
            origin.append(1) //Origin to get hold values which will be used in iteration
            org=0 //Helps in backtracking

        elif (self.is_valid_move(row+1,col+2) and org<2): //Checks if the org is less than 2
            row+=1
            col+=2
            self.board[row][col] = count
            count+=1
            stack.append(row)
            stack.append(col)
            origin.append(2)
            org=0

        elif (self.is_valid_move(row-1,col+2) and org<3):
            row-=1
            col+=2
            self.board[row][col] = count
            count+=1
            stack.append(row)
            stack.append(col)
            origin.append(3)
            org=0

        elif (self.is_valid_move(row-2,col+1) and org<4):
            row-=2
            col+=1
            self.board[row][col] = count
            count+=1
            stack.append(row)
            stack.append(col)
            origin.append(4)
            org=0

        elif (self.is_valid_move(row-2,col-1) and org<5):
            row-=2
            col-=1
            self.board[row][col] = count
            count+=1
            stack.append(row)
            stack.append(col)
            origin.append(5)
            org=0

        elif (self.is_valid_move(row-1,col-2) and org<6):
            row-=1
            col-=2
            self.board[row][col] = count
            count+=1
            stack.append(row)
            stack.append(col)
            origin.append(6)
            org=0

        elif (self.is_valid_move(row+1,col-2) and org<7):
            row+=1
            col-=2
            self.board[row][col] = count
            count+=1
            stack.append(row)
            stack.append(col)
            origin.append(7)
            org=0

        elif (self.is_valid_move(row+2,col-1) and org<8):
            row+=2
            col-=1
            self.board[row][col] = count
            count+=1
            stack.append(row)
            stack.append(col)
            origin.append(8)
            org=0

        else:
            self.board[row][col] = 0
            count-=1
            stack.pop()
            stack.pop()
            col = stack.pop()
            row = stack.pop()
            stack.append(row)
            stack.append(col)
            org=origin.pop()

这是一个顽皮。我检查了 5 * 5木板和6 * 6 ,请检查7和8