結果

問題 No.197 手品
ユーザー lam6er
提出日時 2025-03-31 17:43:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 44 ms / 1,000 ms
コード長 1,436 bytes
コンパイル時間 356 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 56,040 KB
最終ジャッジ日時 2025-03-31 17:43:55
合計ジャッジ時間 3,895 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
from collections import deque

# Precompute the shortest distance for all permutations using BFS
start = (0, 1, 2)
distance = {start: 0}
queue = deque([start])

while queue:
    current = queue.popleft()
    # Generate next states by applying L and R operations
    # L: swap positions 0 and 1
    l_next = list(current)
    l_next[0], l_next[1] = l_next[1], l_next[0]
    l_tuple = tuple(l_next)
    if l_tuple not in distance:
        distance[l_tuple] = distance[current] + 1
        queue.append(l_tuple)
    # R: swap positions 1 and 2
    r_next = list(current)
    r_next[1], r_next[2] = r_next[2], r_next[1]
    r_tuple = tuple(r_next)
    if r_tuple not in distance:
        distance[r_tuple] = distance[current] + 1
        queue.append(r_tuple)

# Read input
s_before = input().strip()
n = int(input())
s_after = input().strip()

# Find all permutations p where applying p to s_before results in s_after
candidates = []
for p in itertools.permutations([0, 1, 2]):
    valid = True
    for i in range(3):
        if s_before[p[i]] != s_after[i]:
            valid = False
            break
    if valid:
        candidates.append(p)

if not candidates:
    print("SUCCESS")
else:
    failure = False
    for p in candidates:
        m = distance[p]
        if m <= n and (m % 2) == (n % 2):
            print("FAILURE")
            failure = True
            break
    if not failure:
        print("SUCCESS")
0