結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー c-yanc-yan
提出日時 2021-03-12 01:32:50
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 926 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-21 13:33:39
合計ジャッジ時間 1,543 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
10,752 KB
testcase_01 AC 30 ms
10,752 KB
testcase_02 AC 36 ms
10,752 KB
testcase_03 AC 37 ms
10,752 KB
testcase_04 AC 32 ms
10,752 KB
testcase_05 AC 32 ms
10,752 KB
testcase_06 AC 30 ms
10,752 KB
testcase_07 AC 28 ms
10,880 KB
testcase_08 AC 28 ms
10,752 KB
testcase_09 AC 28 ms
10,752 KB
testcase_10 AC 28 ms
10,752 KB
testcase_11 AC 27 ms
10,752 KB
testcase_12 AC 34 ms
10,752 KB
testcase_13 AC 34 ms
10,880 KB
testcase_14 AC 32 ms
10,880 KB
testcase_15 AC 36 ms
10,624 KB
testcase_16 AC 32 ms
10,752 KB
testcase_17 AC 32 ms
10,880 KB
testcase_18 AC 34 ms
10,752 KB
testcase_19 AC 32 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

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