結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー maimai
提出日時 2017-05-24 23:41:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 100 ms / 5,000 ms
コード長 1,332 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 81,800 KB
実行使用メモリ 76,568 KB
最終ジャッジ日時 2023-10-19 22:40:03
合計ジャッジ時間 2,590 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
76,564 KB
testcase_01 AC 43 ms
53,620 KB
testcase_02 AC 50 ms
61,440 KB
testcase_03 AC 95 ms
76,564 KB
testcase_04 AC 49 ms
61,440 KB
testcase_05 AC 71 ms
70,240 KB
testcase_06 AC 55 ms
66,032 KB
testcase_07 AC 49 ms
61,440 KB
testcase_08 AC 84 ms
76,060 KB
testcase_09 AC 65 ms
70,548 KB
testcase_10 AC 56 ms
66,256 KB
testcase_11 AC 98 ms
76,200 KB
testcase_12 AC 95 ms
76,568 KB
testcase_13 AC 94 ms
76,564 KB
testcase_14 AC 70 ms
72,644 KB
testcase_15 AC 98 ms
76,200 KB
testcase_16 AC 60 ms
68,384 KB
testcase_17 AC 81 ms
76,056 KB
testcase_18 AC 100 ms
76,200 KB
testcase_19 AC 80 ms
76,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import copy

goal = list(map(lambda e:e%16,  range(1,17)))

def yes():
    print("Yes")
    sys.exit()

def dfs(a, moved,dep=0):
    # print(dep,a,moved)
    if a == goal: yes()
    idx = a.index(0)
    x = idx%4
    y = idx/4
    if 0<x and not moved[a[idx-1]]:
        moved[a[idx-1]] = 1
        a[idx-1], a[idx] = a[idx], a[idx-1]
        dfs(copy.copy(a), copy.copy(moved), dep+1)
        a[idx-1], a[idx] = a[idx], a[idx-1]
        moved[a[idx-1]] = 0
    if x<3 and not moved[a[idx+1]]:
        moved[a[idx+1]] = 1
        a[idx+1], a[idx] = a[idx], a[idx+1]
        dfs(copy.copy(a), copy.copy(moved), dep+1)
        a[idx+1], a[idx] = a[idx], a[idx+1]
        moved[a[idx+1]] = 0
    if 0<y and not moved[a[idx-4]]:
        moved[a[idx-4]] = 1
        a[idx-4], a[idx] = a[idx], a[idx-4]
        dfs(copy.copy(a), copy.copy(moved), dep+1)
        a[idx-4], a[idx] = a[idx], a[idx-4]
        moved[a[idx-4]] = 0
    if y<3 and not moved[a[idx+4]]:
        moved[a[idx+4]] = 1
        a[idx+4], a[idx] = a[idx], a[idx+4]
        dfs(copy.copy(a), copy.copy(moved), dep+1)
        a[idx+4], a[idx] = a[idx], a[idx+4]
        moved[a[idx+4]] = 0

x = list(map(int,input().split()))
x+= list(map(int,input().split()))
x+= list(map(int,input().split()))
x+= list(map(int,input().split()))

dfs(x,[0]*16)

print("No")

0