結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー FromBooskaFromBooska
提出日時 2023-03-07 20:44:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 44 ms / 5,000 ms
コード長 957 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 81,760 KB
実行使用メモリ 55,528 KB
最終ジャッジ日時 2023-10-18 05:35:48
合計ジャッジ時間 2,107 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,528 KB
testcase_01 AC 42 ms
55,528 KB
testcase_02 AC 42 ms
55,528 KB
testcase_03 AC 41 ms
55,528 KB
testcase_04 AC 42 ms
55,528 KB
testcase_05 AC 41 ms
55,528 KB
testcase_06 AC 42 ms
55,528 KB
testcase_07 AC 42 ms
55,528 KB
testcase_08 AC 42 ms
55,528 KB
testcase_09 AC 41 ms
55,528 KB
testcase_10 AC 42 ms
55,528 KB
testcase_11 AC 42 ms
55,528 KB
testcase_12 AC 44 ms
55,528 KB
testcase_13 AC 43 ms
55,528 KB
testcase_14 AC 42 ms
55,528 KB
testcase_15 AC 43 ms
55,528 KB
testcase_16 AC 42 ms
55,528 KB
testcase_17 AC 42 ms
55,528 KB
testcase_18 AC 43 ms
55,528 KB
testcase_19 AC 44 ms
55,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 0の上下左右に、0のところにあるべき駒があるなら動かす、なければNo
# 動かして全盤面正しければYes
# 繰り返す

A = []
for i in range(4):
    temp = list(map(int, input().split()))
    A.append(temp)
    for j in range(4):
        if A[i][j] == 0:
            zero = (i, j)

B = [[0]*4 for i in range(4)]
num = 1
for i in range(4):
    for j in range(4):
        if i == 3 and j == 3:
            continue
        B[i][j] = num
        num += 1

from collections import deque
que = deque()
que.append(zero)
d = [[1, 0], [-1, 0], [0, 1], [0, -1]]
while que:
    ci, cj = que.popleft()
    for di, dj in d:
        if 0 <= ci+di < 4 and 0 <= cj+dj < 4:
            if A[ci+di][cj+dj] == B[ci][cj]:
                A[ci][cj], A[ci+di][cj+dj] = A[ci+di][cj+dj], A[ci][cj]
                que.append((ci+di, cj+dj))
                break
    #print('A', A)
    if A == B:
        print('Yes')
        exit()
print('No')
0