蓝桥杯AnotherEightPuzzle,给出的例题呢能过,为啥过不了检测?

蓝桥杯AnotherEightPuzzle,给出的例题呢能过,为啥过不了检测?有没有会的大佬,帮忙解答一下!!!
是不是它的题目答案有问题啊!!!python代码还出现段错误。。。。6
https://www.lanqiao.cn/problems/3599/learning/?page=1&first_category_id=1&tag_category_id=10&sort=difficulty&asc=1

python代码

import os
import sys
from copy import deepcopy

'''
 A0 B1 C2 D3 E4 F5 G6 H7
0 -> 1, 2, 3
1 -> 0, 2, 4, 5
2 -> 0, 1, 3, 4, 5, 6
3 -> 0, 2, 5, 6
4 -> 1, 2, 5, 7
5 -> 1, 2, 3, 4, 6, 7
6 -> 2, 3, 5, 7
7 -> 4, 5, 6
'''


def checkIsPut(index, num, w):
    """
    检测能否放入该数
    """
    global paths
    for i in paths[index]:
        if w[i] != 0 and abs(num - w[i]) == 1:
            return 0
    return 1


def dfs(index, w):
    global visit
    if index == 8:
        res.append(w)
        # print(w)
        return

    # print(w, visit, checkIsPut(index, 7, w), index, visit[index])
    # for i in range(left, 8):
    if visit[index] == 1:
        dfs(index + 1, w)
    else:
        for j in range(1, 9):
            if j not in w and checkIsPut(index, j, w) == 1:
                w[index] = j
                visit[index] = 1
                dfs(index + 1, w.copy())
                w[index] = 0
                visit[index] = 0


way_count = 0
visit = [0] * 8
paths = [[1, 2, 3], [0, 2, 4, 5], [0, 1, 3, 4, 5, 6], [0, 2, 5, 6], [1, 2, 5, 7], [1, 2, 3, 4, 6, 7],
         [2, 3, 5, 7], [4, 5, 6]]
res = []
T = int(input())
for i in range(T):
    way_count = 0
    way = list(map(int, input().split()))
    # way = [7, 0, 0, 0, 0, 0, 0, 0]
    for j, k in enumerate(way):
        if k != 0:
            visit[j] = 1

    dfs(0, way)
    # dfs(0, way, zero_i, 0)
    print(res)
    if len(res) == 1:
        p_w = map(str, res[0])
        print(f"Case {i}: {' '.join(p_w)}")
    elif len(res) > 1:
        print(f"Case {i}: Not unique")
    else:
        print(f"Case {i}: No answer")

posted @ 2024-03-28 18:08  晓星晨曦  阅读(14)  评论(0编辑  收藏  举报