結果

問題 No.3 ビットすごろく
ユーザー tnodino
提出日時 2022-05-26 18:08:21
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 556 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-09-20 15:03:52
合計ジャッジ時間 1,902 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def bfs():
    INF = 1<<32
    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()
if cost[N] == INF:
    cost[N] = -1
print(cost[N])
0