程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了试图数数的错误。从网格上的 A 点到 B 点所需的移动数。不适用于所有值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决试图数数的错误。从网格上的 A 点到 B 点所需的移动数。不适用于所有值?

开发过程中遇到试图数数的错误。从网格上的 A 点到 B 点所需的移动数。不适用于所有值的问题如何解决?下面主要结合日常开发的经验,给出你关于试图数数的错误。从网格上的 A 点到 B 点所需的移动数。不适用于所有值的解决方法建议,希望对你解决试图数数的错误。从网格上的 A 点到 B 点所需的移动数。不适用于所有值有所启发或帮助;

我正在挑战计算从 A 点到 B 点所需的移动次数,该网格像棋盘一样布置,您可以进行的移动是骑士的移动,因此 2 英寸任意方向和1个垂直。

我已经解决了大部分问题,但由于某种原因,我的计数器没有返回两点之间的移动次数。以下是我对计数的看法。

您会注意到我使用了一个名为 position 的字典,这样做的原因是我可以存储一个 int 值,表示该特定位置从目的地开始的移动次数。

我想最后我应该在移动被认为有效后增加移动值,但我仍然没有得到正确的数字。

def solution(src,dest):
# Chessboard made using nested Lists. The indexes will act as coordinates.
chessboard = [
    [0,1,2,3,4,5,6,7],[8,9,10,11,12,13,14,15],[16,17,18,19,20,21,22,23],[24,25,26,27,28,29,30,31],[32,33,34,35,36,37,38,39],[40,41,42,43,44,45,46,47],[48,49,50,51,52,53,54,55],[56,57,58,59,60,61,62,63]
    ]

# Find index values of src and dest
for row in chessboard:
    if src in row:
        srcX = chessboard.index(row)
        srcY = row.index(srC)
    if Dest in row:
        destX = chessboard.index(row)
        destY = row.index(dest)
    
# position Dict to store indexes and no of mvoes when using bfs
position = {
    'x': 0,'y': 0,'moves': 0,}

position['x'] = srcX
position['y'] = srcY

# Below represents the knights moves to be applIEd to the index of position
row = [-2,-2,-1,-1]
col = [-1,-2]


# We use an if-statement to check for valID moves 
def isValID(x,y):
    return not (x < 0 or y < 0 or x >=8 or y >=8)
    

q = []
q.append(position)

# Record spaces visited already
isVisited = []

while len(q)>0:
    space = q.pop()
    x = space['x']
    y = space['y']
    moves = space['moves']
    
    
    # if the position matches the desTination,return no.moves
    # I'm just using print to see the result in the terminal
    if x == destX and y == destY:
        print(moves)
        
   
    if (x,y) not in isVisited:
        isVisited.append((x,y))
    
        
        # Loop over possible moves
        for i in range(len(row)):
            newX = x + row[i]
            newY = y + col[i]

            
            if isValID(newX,newY):
                position['x'] = newX
                position['y'] = newY
                position['moves'] = moves+1
                q.append(position)

解决方法

这是一个常见的 Python 问题。您创建一个名为“位置”的字典并将其添加到队列中。然后,每当您有新动作时,您都可以修改同一个字典并将其再次添加到队列中,但这并不会创建新字典。你最终会得到一个队列,里面充满了对完全相同的字典的引用。每次更改“位置”时,都会更改队列中的每个字典。每次都需要创建一个新对象

这似乎有效。

# We use an if-statement to check for valid moves 
def isValid(x,y):
    return (0 <= x <= 7) and (0 <= y <= 7)

def solution(src,dest):
# Chessboard made using nested lists. The indexes will act as coordinates.
    chessboard = [
        [0,1,2,3,4,5,6,7],[8,9,10,11,12,13,14,15],[16,17,18,19,20,21,22,23],[24,25,26,27,28,29,30,31],[32,33,34,35,36,37,38,39],[40,41,42,43,44,45,46,47],[48,49,50,51,52,53,54,55],[56,57,58,59,60,61,62,63]
        ]

# Find index values of src and dest

    srcY = src % 8
    srcX = src // 8
    destY = dest % 8
    destX = dest // 8
        
# Position Dict to store indexes and no of mvoes when using bfs
    position = {
        'x': srcX,'y': srcY,'moves': 0,}

# Below represents the knights moves to be applied to the index of position
    row = [ -2,-2,-1,2]
    col = [ -1,1]

    q = []
    q.append(position)

# Record spaces visited already
    isVisited = []

    while q:
        space = q.pop()
        print( "checking",space )
        x = space['x']
        y = space['y']
        moves = space['moves']
        
        
        # if the position matches the desTination,return no.moves
        # I'm just using print to see the result in the terminal
        if x == destX and y == destY:
            print(f"{moves}!!!")
            return moves
       
        if (x,y) not in isVisited:
            isVisited.append((x,y))
        
            # Loop over possible moves
            for dx,dy in zip(row,col):
                newX = x + dx
                newY = y + dy
                
                if isValid(newX,newY):
                    position = {
                        'x': newX,'y': newY,'moves': moves+1
                    }
                    q.append(position)

print( solution( 3,61 ) )

大佬总结

以上是大佬教程为你收集整理的试图数数的错误。从网格上的 A 点到 B 点所需的移动数。不适用于所有值全部内容,希望文章能够帮你解决试图数数的错误。从网格上的 A 点到 B 点所需的移动数。不适用于所有值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。