結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー c-yanc-yan
提出日時 2021-03-12 01:32:50
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 926 bytes
コンパイル時間 110 ms
コンパイル使用メモリ 11,084 KB
実行使用メモリ 8,392 KB
最終ジャッジ日時 2023-08-03 15:22:15
合計ジャッジ時間 1,587 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,260 KB
testcase_01 AC 15 ms
8,392 KB
testcase_02 AC 21 ms
8,364 KB
testcase_03 AC 23 ms
8,232 KB
testcase_04 AC 21 ms
8,380 KB
testcase_05 AC 18 ms
8,232 KB
testcase_06 AC 15 ms
8,276 KB
testcase_07 AC 18 ms
8,240 KB
testcase_08 AC 18 ms
8,232 KB
testcase_09 AC 16 ms
8,264 KB
testcase_10 AC 16 ms
8,276 KB
testcase_11 AC 15 ms
7,832 KB
testcase_12 AC 21 ms
8,228 KB
testcase_13 AC 21 ms
8,288 KB
testcase_14 AC 20 ms
8,364 KB
testcase_15 AC 22 ms
8,356 KB
testcase_16 AC 21 ms
8,364 KB
testcase_17 AC 20 ms
8,232 KB
testcase_18 AC 22 ms
8,328 KB
testcase_19 AC 20 ms
8,292 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