結果

問題 No.3 ビットすごろく
ユーザー nanae
提出日時 2017-02-22 19:25:35
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 651 bytes
コンパイル時間 112 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-07-01 08:18:40
合計ジャッジ時間 2,334 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N = int(input())
d = [-1] * N
checked = [False] * N
p = [None] * N
nxts = deque([0])
checked[0] = True

while nxts:
    pos = nxts.popleft()

    if pos == 0:
        d[pos] = 1
    else:
        d[pos] = d[p[pos]] + 1

    if pos == N - 1:
        print(d[N - 1])
        quit()

    step = bin(pos + 1).count('1')

    if pos - step >= 0 and not checked[pos - step]:
        nxts.append(pos - step)
        checked[pos - step] = True
        p[pos - step] = pos
    if pos + step < N and not checked[pos + step]:
        nxts.append(pos + step)
        checked[pos + step] = True
        p[pos + step] = pos

print(-1)
0