結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー NoneNone
提出日時 2021-05-08 20:45:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 113 ms / 5,000 ms
コード長 1,435 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 81,872 KB
実行使用メモリ 78,508 KB
最終ジャッジ日時 2023-10-17 06:21:20
合計ジャッジ時間 3,076 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
78,468 KB
testcase_01 AC 36 ms
53,508 KB
testcase_02 AC 44 ms
61,836 KB
testcase_03 AC 109 ms
78,468 KB
testcase_04 AC 37 ms
53,508 KB
testcase_05 AC 36 ms
53,508 KB
testcase_06 AC 93 ms
77,768 KB
testcase_07 AC 84 ms
77,652 KB
testcase_08 AC 60 ms
70,364 KB
testcase_09 AC 76 ms
77,100 KB
testcase_10 AC 88 ms
77,780 KB
testcase_11 AC 91 ms
77,480 KB
testcase_12 AC 110 ms
78,468 KB
testcase_13 AC 109 ms
78,468 KB
testcase_14 AC 90 ms
77,692 KB
testcase_15 AC 90 ms
77,480 KB
testcase_16 AC 93 ms
77,756 KB
testcase_17 AC 91 ms
77,628 KB
testcase_18 AC 113 ms
78,508 KB
testcase_19 AC 91 ms
77,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def dfs(start=0,goal=None):
    parents={} # parents=[-1]*N にする場合は if q in parents も変える
    p,t=start,0
    grid,used=tuple(p[0]),tuple(p[1])
    parents[(grid,used)]=-2
    next_set=[(p,t)]
    if grid==goal:
        return t
    while next_set:
        p,t=next_set.pop()
        for q in make_edges(p):
            grid,used=tuple(q[0]),tuple(q[1])
            if (grid,used) in parents:
                continue
            if grid==goal:
                return t+1
            parents[(grid,used)]=tuple(p[0]),tuple(p[1])
            next_set.append((q,t+1))
    return -1


def make_edges(p):
    grid, used=p
    for h0 in range(H):
        for w0 in range(W):
            if grid[h0*W+w0]==0:
                for dh,dw in ((-1,0),(1,0),(0,-1),(0,1)):
                    h,w=h0+dh,w0+dw
                    if h>=H or w>=W or h<0 or w<0:
                        continue
                    if used[h*W+w]==1:
                        continue
                    grid[h0*W+w0],grid[h*W+w] = grid[h*W+w],grid[h0*W+w0]
                    used[h0*W+w0]=1
                    yield grid[:],used[:]
                    used[h0*W+w0]=0
                    grid[h0*W+w0],grid[h*W+w] = grid[h*W+w],grid[h0*W+w0]

H=W=4
start=[]
goal=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0)
for _ in range(4):
    start.extend(map(int, input().split()))

res=dfs((start,[0]*16),goal)
print("Yes" if res!=-1 else "No")
0