骑士之旅中的堆栈实现[关闭]

Posted

技术标签:

【中文标题】骑士之旅中的堆栈实现[关闭]【英文标题】:stack implementation in knight tour [closed] 【发布时间】:2022-01-17 00:25:40 【问题描述】:

骑士之旅是骑士在棋盘上的一系列移动,使得 骑士恰好访问每个广场一次。适用标准国际象棋规则。一个骑士可以移动两个 一个方向的正方形和一个水平或垂直的正方形,以任一顺序。给定 一个整数 n,求一个骑士在 n x n 棋盘上的游历

【问题讨论】:

你有什么问题? 先生仅使用堆栈来解决它我无法弄清楚。使用递归或回溯可以做到这一点,我在你的管子上找到了它的视频,但只使用无法计算的堆栈 【参考方案1】:
#Here first we defined a stack class for use in KnightRoute class
'''The time complexity of this programm is O(8^n^2)'''
class Stack:

    def __init__(self):
        self.array = []    # define a empty list
        self.size = 0

    def push(self, element):
        self.array.append(element)                   #here we defined a push function for stack
        self.size += 1

    def pop(self):
        if self.size < 1:                        # here we defined a Pop function for stack
            print("No element to pop")
        else:
            self.size -= 1
            return self.array.pop(-1)

    def top(self):                        # here we defined a top function for stack for top element in stack
        if self.size < 1:
            return ""
        return self.array[-1]



def KnightsRoute(n):           # Make a KnightRoute class for our problem

    # Stack to keep track of moves that we are considering as solution
    moves = Stack()

    # Directions in which a knight can move
    directions = [[2, 1],[2, -1],[-2, 1],[-2, -1],[1, 2],[-1, 2],[1, -2],[-1, -2],]

     # Here we make a list of lists with False element visited or not
    vis = [[False for i in range(n)] for j in range(n)]

    # 2-D array to tell as the time at which we reached that particular cell
    graph = [[0 for i in range(n)] for j in range(n)]

    # Checking validity of a current move, current move should place knight somewhere inside the chessboard and that cell should be unvisited before we place knight in it
    def available(i, j):
        return i < n and j < n and i > -1 and j > -1 and not vis[i][j]

    # DFS on a cell to all possible moves and checking whether it would lead to a solution
    # si, sj are the coordinates of the current cell we will DFS from
    # cur is varibale to tell us how many cells are visited before this one
    def dfs(si, sj, cur=1):
        global ans

        # If the cells visited are equal to the number of cells in the chessboard, then we have in fact visited all cells and reached a solution
        if cur >= n * n:
            ans = True

        # Marking current cell as visited and pushing it in the solution stack
        vis[si][sj] = True
        moves.push((si, sj))

        # Calculating all other reachable cells from the current cell
        for a in directions:
            i = si + a[0]
            j = sj + a[1]

            # If current cell is valid, the we will DFS from it 
            if available(i, j):
                dfs(i, j, cur + 1)

            # If we have alreadily found and answer, we will stop searching further
            if ans:
                graph[si][sj] = cur
                return

        # We visited current and now we might want to check it again for some other moves that we used before it, so we will pop it for now from the solution and mark it as unvisited
        moves.pop()
        vis[si][sj] = False

    
    # If answer exists,it could be found from any cell, so we can assume (1, 1) as the starting point for the search
    dfs(0, 0)

    if not ans:
        print("Solution is not exist")
        
    for i in graph:
        print(i)


n = int(input("Enter the size of board : "))
ans=False
KnightsRoute(n)

print("Thank You")

'''IMPORTANT: The time complexity of this code is very high if we increase the size of input then it take much time to show output 
,check the code up to input 7 for earlier output'''

                                           #THANKS FOR RUN THE CODE```


【讨论】:

以上是关于骑士之旅中的堆栈实现[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

使用递归的 C++ 中的骑士之旅

c ++中的递归回溯骑士之旅

平行骑士之旅算法

如何实现“向右拖动以关闭”导航堆栈中的视图控制器?

如何解决具有特殊约束的部分骑士之旅

陷入无限循环(骑士之旅问题)