結果

問題 No.2984 [Cherry Anniversary 4] 満開の願いを込めた 27 の桜
ユーザー ShirotsumeShirotsume
提出日時 2024-12-07 00:14:38
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,193 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 89,996 KB
最終ジャッジ日時 2024-12-08 23:31:33
合計ジャッジ時間 50,390 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 258 ms
81,040 KB
testcase_01 AC 2,115 ms
86,416 KB
testcase_02 AC 2,122 ms
86,416 KB
testcase_03 AC 2,115 ms
86,800 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2,119 ms
86,796 KB
testcase_14 AC 2,099 ms
86,536 KB
testcase_15 AC 2,104 ms
86,800 KB
testcase_16 AC 2,082 ms
87,048 KB
testcase_17 AC 2,096 ms
86,420 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random
from collections import deque, Counter, defaultdict
def debug(*x):print('debug:',*x, file=sys.stderr)
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 61 - 1
mod = 998244353

#3*3*3のマス目に対し、行を入れ替える操作繰り返してできるマス目を列挙

S = [[[(i, j, k) for k in range(3)] for j in range(3)] for i in range(3)]

def swap1(S, i, j):
    S = list(list(list(x) for x in y) for y in S)
    S[i], S[j] = S[j], S[i]
    return S
def swap2(S, i, j):
    S = list(list(list(x) for x in y) for y in S)
    for k in range(3):
        S[k][i], S[k][j] = S[k][j], S[k][i]
    return S
def swap3(S, i, j):
    S = list(list(list(x) for x in y) for y in S)
    for k in range(3):
        for l in range(3):
            S[k][l][i], S[k][l][j] = S[k][l][j], S[k][l][i]
    return S
allsigma = set()
cost = defaultdict(lambda : inf)

Q = deque()
Q.append(S)
cost[tuple(tuple(tuple(x) for x in y) for y in S)] = 0
while Q:
    S = Q.popleft()
    c = cost[tuple(tuple(tuple(x) for x in y) for y in S)]
    for i in range(3):
        for j in range(i+1, 3):
            T = swap1(tuple(tuple(tuple(x) for x in y) for y in S), i, j)
            if cost[tuple(tuple(tuple(x) for x in y) for y in T)] > c + 1:
                cost[tuple(tuple(tuple(x) for x in y) for y in T)] = c + 1
                allsigma.add(tuple(tuple(tuple(x) for x in y) for y in T))
                Q.append(T)
            T = swap2(tuple(tuple(tuple(x) for x in y) for y in S), i, j)
            if cost[tuple(tuple(tuple(x) for x in y) for y in T)] > c + 1:
                cost[tuple(tuple(tuple(x) for x in y) for y in T)] = c + 1
                allsigma.add(tuple(tuple(tuple(x) for x in y) for y in T))
                Q.append(T)
            T = swap3(tuple(tuple(tuple(x) for x in y) for y in S), i, j)
            if cost[tuple(tuple(tuple(x) for x in y) for y in T)] > c + 1:
                cost[tuple(tuple(tuple(x) for x in y) for y in T)] = c + 1
                allsigma.add(tuple(tuple(tuple(x) for x in y) for y in T))
                Q.append(T)
def solve():
    A = [[li() for _ in range(3)] for _ in range(3)]
    X = [li() for _ in range(3)]
    Y = [li() for _ in range(3)]
    Z = [li() for _ in range(3)]
    ans = inf
    for S in allsigma:
        B = [[[0]*3 for _ in range(3)] for _ in range(3)]
        for i in range(3):
            for j in range(3):
                for k in range(3):
                    B[i][j][k] = A[S[i][j][k][0]][S[i][j][k][1]][S[i][j][k][2]]
        #X,Y,Zの値を検算
        ok = True
        for i in range(3):
            for j in range(3):
                if B[0][i][j] + B[1][i][j] + B[2][i][j] != X[i][j]:
                    ok = False
                if B[i][0][j] + B[i][1][j] + B[i][2][j] != Y[i][j]:
                    ok = False  
                if B[i][j][0] + B[i][j][1] + B[i][j][2] != Z[i][j]:
                    ok = False
        if ok:
            ans = min(ans, cost[tuple(tuple(tuple(x) for x in y) for y in S)])
    print(ans if ans != inf else -1)
    
    
for _ in range(ii()):
    solve()
0