Knight's Move隐藏测试案例4失败(Google Foobar测试)

问题描述 投票:0回答:1

隐藏测试用例4

我正在进行 Google foobar 测试的任务 2。任务是找到使用 Knight 的移动从起始节点到达目标节点的最少移动次数。代码运行良好并给出预期的输出。但有一个隐藏的测试用例 4 失败了,我不明白为什么。

from itertools import product
def solution(src, dest):
    puzzle = [[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]]
    if (src not in range(0,64) or dest not in range(0,64)) :
        raise Exception('out of range')
    step = 1
    row_start = 0
    r_e = 7
    str_r = 0
    str_c = 0
    while src:
        if r_s <= src <= r_e:
            for i in range(0, 8):
                if puzzle[str_r][i] == src:
                    str_c = i
                    break
            break
        str_r += 1
        r_s += 8
        r_e += 8
    moves = list(product([str_r - 1, str_r + 1], [str_c - 2, str_c + 2])) + list(product([str_r - 2, str_r + 2],[str_c - 1, str_c + 1]))
    m_s = [(x,y) for x,y in moves if x >= 0 and y >= 0 and x < 8 and y < 8]
    for i, j in m_s:
        if puzzle[i][j] == dest:
            move_counter = step
            return move_counter
    while True:
        step += 1
        List_steps = []
        for i, j in m_s:
            r_s = 0
            r_e = 7
            str_r = 0
            str_c = 0
            while True:
                if r_s <= puzzle[i][j] <= r_e:
                    for x in range(0, 8):
                        if puzzle[str_r][x] == puzzle[i][j]:
                            str_c = x
                            break
                    break
                str_r += 1
                r_s += 8
                r_e += 8
            moves = list(product([str_r - 1, str_r + 1], [str_c - 2, str_c + 2])) + list(product([str_r - 2, str_r + 2],[str_c - 1, str_c + 1]))
            m_z = [(x,y) for x,y in moves if x >= 0 and y >= 0 and x < 8 and y < 8]
            for m, n in m_z:
                if puzzle[m][n] == dest:
                    return step
            move_counter = 'Not Found'
            List_steps += m_z
        if move_counter == 'Not Found':
            m_s = list(set(List_steps))
            continue

if __name__ == '__main__':
    start = int(input("Input Source :"))
    end = int(input("Input Destination :"))
    print(solution(start, end))

问题

向我建议您认为我应该更改以通过此测试用例的任何内容。 这几天我一直被这个问题困扰

python-3.x depth-first-search breadth-first-search knights-tour
1个回答
0
投票

我被困在同一个测试用例上,结果我忘记检查 src == dest 是否存在,如果是的话返回 0。希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.