結果

問題 No.3 ビットすごろく
ユーザー Kgm1500
提出日時 2018-06-12 20:14:01
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 734 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-07-01 09:02:50
合計ジャッジ時間 2,221 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

# N = 11
from collections import deque

N = int(input())

que = deque([])
now = 1
que.append(now)
visited = [False for i in range(N+1)]
step = [-1 for i in range(N+1)]
visited[1] = True
step[1] = 1
step_count = 1

while len(que) > 0 and now != N:
    now = que.popleft()
    step_count = step[now] + 1
    step_width = bin(now).count("1")
    next_f = now + step_width
    next_b = now - step_width
    if next_f <= N and visited[next_f] is False:
        que.append(next_f)
        visited[next_f] = True
        step[next_f] = step_count
    if next_b >= 1 and visited[next_b] is False:
        que.append(next_b)
        visited[next_b] = True
        step[next_b] = step_count

if now == N:
    print(step[N])
else:
    print(-1)
0