結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー rpy3cpprpy3cpp
提出日時 2015-06-19 23:00:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 883 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 10,944 KB
実行使用メモリ 8,368 KB
最終ジャッジ日時 2023-09-21 10:05:34
合計ジャッジ時間 1,209 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,192 KB
testcase_01 AC 15 ms
8,320 KB
testcase_02 AC 16 ms
8,180 KB
testcase_03 AC 17 ms
8,180 KB
testcase_04 AC 17 ms
8,240 KB
testcase_05 AC 16 ms
8,312 KB
testcase_06 AC 17 ms
8,300 KB
testcase_07 AC 15 ms
8,252 KB
testcase_08 AC 17 ms
8,244 KB
testcase_09 AC 16 ms
8,308 KB
testcase_10 AC 16 ms
7,996 KB
testcase_11 AC 15 ms
8,320 KB
testcase_12 AC 16 ms
8,368 KB
testcase_13 AC 16 ms
8,304 KB
testcase_14 AC 17 ms
8,180 KB
testcase_15 AC 16 ms
8,252 KB
testcase_16 AC 17 ms
8,256 KB
testcase_17 AC 17 ms
8,192 KB
testcase_18 AC 17 ms
7,864 KB
testcase_19 AC 16 ms
8,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    a = []
    for i in range(4):
        row = list(map(int, input().split()))
        a.append(row)
    return a

def solve(a):
    init = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,0]]
    moved = [False] * 16
    r, c = 3, 3
    return dfs(a, init, moved, r, c)

def dfs(goal, status, moved, r, c):
    if goal == status:
        return True
    for nr, nc in [(r+1, c), (r-1, c), (r, c+1), (r, c-1)]:
        if nr > 3 or nr < 0 or nc > 3 or nc < 0:
            continue
        num = status[nr][nc]
        if moved[num]:
            continue
        status[nr][nc] = 0
        status[r][c] = num
        moved[num] = True
        if dfs(goal, status, moved, nr, nc):
            return True
        moved[num] = False
        status[r][c] = 0
        status[nr][nc] = num
    return False

a = read_data()
if solve(a):
    print('Yes')
else:
    print('No')
0