結果

問題 No.3 ビットすごろく
ユーザー tnodino
提出日時 2022-01-30 13:48:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 559 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 71,180 KB
最終ジャッジ日時 2024-06-11 08:13:57
合計ジャッジ時間 3,457 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def bfs(N, INF):
    Cost = [INF] * (N+1)
    Cost[1] = 1
    Queue = deque()
    Queue.append(1)
    while Queue:
        x = Queue.popleft()
        C = bin(x).count('1')
        if 1 <= x - C and Cost[x] + 1 < Cost[x-C]:
            Cost[x-C] = Cost[x] + 1
            Queue.append(x-C)
        if x + C <= N and Cost[x] + 1 < Cost[x+C]:
            Cost[x+C] = Cost[x] + 1
            Queue.append(x+C)
    return Cost

INF = 1<<32
N = int(input())
Cost = bfs(N, INF)
if Cost[N] == INF:
    print(-1)
else:
    print(Cost[N])
0