結果
問題 | No.228 ゆきこちゃんの 15 パズル |
ユーザー |
|
提出日時 | 2017-05-24 23:41:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 90 ms / 5,000 ms |
コード長 | 1,332 bytes |
コンパイル時間 | 333 ms |
コンパイル使用メモリ | 82,284 KB |
実行使用メモリ | 77,056 KB |
最終ジャッジ日時 | 2024-09-19 18:39:06 |
合計ジャッジ時間 | 2,430 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
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")