結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー matsu7874matsu7874
提出日時 2015-11-29 17:58:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 1,561 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 11,072 KB
実行使用メモリ 8,868 KB
最終ジャッジ日時 2023-10-12 05:44:28
合計ジャッジ時間 1,623 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
8,836 KB
testcase_01 AC 21 ms
8,600 KB
testcase_02 AC 20 ms
8,668 KB
testcase_03 AC 24 ms
8,752 KB
testcase_04 AC 19 ms
8,616 KB
testcase_05 AC 20 ms
8,560 KB
testcase_06 AC 22 ms
8,808 KB
testcase_07 AC 22 ms
8,776 KB
testcase_08 AC 23 ms
8,704 KB
testcase_09 AC 23 ms
8,724 KB
testcase_10 AC 22 ms
8,764 KB
testcase_11 AC 22 ms
8,684 KB
testcase_12 AC 24 ms
8,684 KB
testcase_13 AC 25 ms
8,752 KB
testcase_14 AC 23 ms
8,788 KB
testcase_15 AC 24 ms
8,684 KB
testcase_16 AC 24 ms
8,868 KB
testcase_17 AC 23 ms
8,840 KB
testcase_18 AC 24 ms
8,736 KB
testcase_19 AC 23 ms
8,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
A = []
for i in range(4):
    A += list(map(int, input().split()))
# print(A)
dq = collections.deque()
dq.append([[False] * 16, A[:]])
while dq:
    moved, puzzle = dq.popleft()
    # print(puzzle,moved)
    if puzzle == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]:
        print('Yes')
        exit()
    zero = puzzle.index(0)
    if zero >= 4:
        t = puzzle[zero - 4]
        if not moved[t]:
            next_puzzle = puzzle[:]
            next_puzzle[zero] = t
            next_puzzle[zero - 4] = 0
            next_moved = moved[:]
            next_moved[t] = True
            dq.append([next_moved, next_puzzle])
    if zero % 4 > 0:
        t = puzzle[zero - 1]
        if not moved[t]:
            next_puzzle = puzzle[:]
            next_puzzle[zero] = t
            next_puzzle[zero - 1] = 0
            next_moved = moved[:]
            next_moved[t] = True
            dq.append([next_moved, next_puzzle])
    if zero < 12:
        t = puzzle[zero + 4]
        if not moved[t]:
            next_puzzle = puzzle[:]
            next_puzzle[zero] = t
            next_puzzle[zero + 4] = 0
            next_moved = moved[:]
            next_moved[t] = True
            dq.append([next_moved, next_puzzle])
    if zero % 4 < 3:
        t = puzzle[zero + 1]
        if not moved[t]:
            next_puzzle = puzzle[:]
            next_puzzle[zero] = t
            next_puzzle[zero + 1] = 0
            next_moved = moved[:]
            next_moved[t] = True
            dq.append([next_moved, next_puzzle])
print('No')
0