繁体   English   中英

带主教的骑士的最小动作

[英]Minimum moves of a knight with bishop

问题是在 n*n 棋盘中,一个骑士从 A 点到 B 点的最小移动(骑士可以在水平方向移动两步,在垂直方向移动一步,或者在水平方向上垂直移动两步)。 棋盘上有一个对角线行走的象,除非象死了或者位置是B点,否则骑士不能移动到象受威胁的位置。 骑士可以选择杀死象(如果它处于它可以前往)并释放所有先前受到威胁的位置。

我在一次在线评估中得到了这个问题,但只有 15 个测试用例中有 10 个是正确的。 我想我可能需要向队列中的元组添加一个布尔值,以表明在最新步骤中主教是否还活着,但为时已晚。

我该如何修改?

from collections import deque
import math

n = 5
startRow = 0
startCol = 0
endRow = 4
endCol = 3
bishopRow = 3
bishopCol = 0
from collections import deque
import math
#
# Complete the 'moves' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER n
#  2. INTEGER startRow
#  3. INTEGER startCol
#  4. INTEGER endRow
#  5. INTEGER endCol
#  6. INTEGER bishopRow
#  7. INTEGER bishopCol
#
def isBishopAlive(n, bishopRow, bishopCol):
    if bishopRow < n and bishopCol < n:
        return True
    else:
        return False
def moves(n, startRow, startCol, endRow, endCol, bishopRow, bishopCol):
    # Write your code here
    x, y = abs(endRow), abs(endCol)
    res = 0
    moves = ((2,1), (1,2), (-1,2), (-2,1), (-2,-1), (-1,-2), (1,-2), (2,-1))
    visited = []
    queue = deque()
    queue.append((startRow, startCol, 0))
    while queue:
        i, j, steps = queue.popleft()
        if i == x and j == y:
            return res + steps
        for di, dj in moves:
            cr = i + di
            cc = j + dj
            if isBishopAlive(n, bishopRow, bishopCol) == True:
                if abs(cr-bishopRow) == abs(cc-bishopCol):
                    if cc != y and cr != x:
                        continue
                if (cr == bishopRow) and (cc == bishopCol):
                    bishopRow, bishopCol = math.inf, math.inf
            if abs(cr) > n-1 or abs(cc) > n-1:
                continue
            if (cr, cc) in visited:
                continue
            if isBishopAlive(n, bishopRow, bishopCol) == True:
                bishop = True
            else:
                bishop = False
            if ((x-i) * di) > 0 or ((y-j) * dj) > 0:
                queue.append([cr, cc, steps+1])
                visited.append((cr, cc))
    return -1    
    

您的代码中存在以下问题:

  • 主教永远不会被捕获,因为当使用crcc到达那个方块时,首先将评估if abs(cr-bishopRow) == abs(cc-bishopCol)条件——并发现为真——然后是continue

  • 当主教被捕获时,您的代码在查看未捕获主教的其他路径时永远不会将其放回原处。 相反,队列中的项目应该包括该状态是否是通过捕获主教来实现的。

  • visited列表中的项目不表明访问是否发生在主教还活着的时候。 然而,这很重要,因为如果你先访问一个方块时主教还活着,然后在主教被俘时再次访问它,第二次访问可能会导致更好的解决方案。 因此,在将正方形标记为已访问时,将活动状态包含在元组中。

  • 该代码允许将crcc负坐标推入队列。 应该避免这种情况。

  • 这种排除远离目标的动作过于乐观:

     if ((xi) * di) > 0 or ((yj) * dj) > 0

    以这个棋盘为例,其中“N”是骑士,“B”是主教,“e”是结束方格:

     ┌───┬───┬───┬───┬───┐ │ │ │ │ │ │ ├───┼───┼───┼───┼───┤ │ │ │ │ │ B │ ├───┼───┼───┼───┼───┤ │ │ │ N │ │ │ ├───┼───┼───┼───┼───┤ │ │ │ │ │ │ ├───┼───┼───┼───┼───┤ │ │ │ │ │ e │ └───┴───┴───┴───┴───┘

    主教必须首先采取行动,然后骑士必须移到最上面一行以获得最佳解决方案。

    我建议删除此条件,否则请确保它不太严格并且永远不会排除最佳路径。

不是问题,但是:

  • res永远不会被赋值为 0 以外的值:不需要这个变量
  • bishop设置,但从未读取:不需要此变量(以及它周围的if...else块)
  • visited最好是一个集合而不是一个列表,所以有更好的时间复杂度。
  • endRowendCol永远不会是负数,因此在xy复制它们的绝对值是没有用的。 只需使用原始参数变量。
  • 当您在将方块放入队列时将方块标记为已访问时,您还应该在进入循环之前对起始方块执行相同操作。
  • 有一个嵌套if导致continue 这些if条件可以合并为一个if条件 using and
  • 两个if在相同的水平的条件,这两个导致continue可以合并成一个if条件使用or
  • (cr == bishopRow)周围的括号不是必需的。
  • 您可以通过在执行移动时检查是否达到目标来节省一些执行时间,而不是等到该移动从队列中弹出。 这只是意味着您必须进行初始检查以查看起始位置是否等于结束位置,但这是值得的。

这是一个更正的版本:

def moves(n, startRow, startCol, endRow, endCol, bishopRow, bishopCol):
    if startRow == endRow and startCol == endCol:  # Deal with trivial case
        return 0
    moves = ((2,1), (1,2), (-1,2), (-2,1), (-2,-1), (-1,-2), (1,-2), (2,-1))
    queue = deque()
    queue.append((startRow, startCol, True, 0))  # Include that bishop is alive
    # Let visited be a set. Mark start as visited and include alive-status
    visited = set([(startRow, startCol, True)])
    while queue:
        i, j, alive, steps = queue.popleft()
        for di, dj in moves:
            cr = i + di
            cc = j + dj
            if cr == endRow and cc == endCol:  # When found, don't bother about queuing it
                return steps + 1 # No need for a res variable 
            # Update alive-state, just for current path
            stillalive = alive and (cr != bishopRow or cc != bishopCol)
            # Neither cr/cc should be negative
            if 0 <= cr < n and 0 <= cc < n and (cr, cc, stillalive) not in visited and (
                    not stillalive or abs(cr - bishopRow) != abs(cc - bishopCol)):
                queue.append((cr, cc, stillalive, steps + 1))  # Append alive-status too
                visited.add((cr, cc, stillalive))  # Visited should depend on alive
    return -1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM