結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー c-yan
提出日時 2021-03-12 01:32:50
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 926 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-10-13 12:11:35
合計ジャッジ時間 1,670 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

a = [list(map(int, input().split())) for _ in range(4)]

used = {0}


def is_ok():
    b = a[0] + a[1] + a[2] + a[3]
    c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]
    return all(x == y for x, y in zip(b, c))

def pos_zero():
    for y in range(4):
        for x in range(4):
            if a[y][x] != 0:
                continue
            return y, x
    

def f():
    if is_ok():
        return True

    y, x = pos_zero()
    for dy, dx in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        ny = y + dy
        nx = x + dx
        if ny < 0 or ny >= 4 or nx < 0 or nx >= 4:
            continue
        v = a[ny][nx]
        if v in used:
            continue
        used.add(v)
        a[y][x] = v
        a[ny][nx] = 0
        ret = f()
        if ret:
            return True
        a[y][x] = 0
        a[ny][nx] = v
        used.remove(v)
    return False


if f():
    print('Yes')
else:
    print('No')
0