結果

問題 No.3 ビットすごろく
コンテスト
ユーザー dango
提出日時 2023-06-14 21:03:51
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 4,384 ms / 5,000 ms
コード長 577 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 168 ms
コンパイル使用メモリ 85,260 KB
実行使用メモリ 310,932 KB
最終ジャッジ日時 2026-03-07 17:22:27
合計ジャッジ時間 42,496 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def min_moves(N: int) -> int:
    from collections import deque
    queue = deque([(1, 0)])
    visited = set()
    while queue:
        pos, moves = queue.popleft()
        if pos == N:
            return moves + 1
        visited.add(pos)
        next_moves = bin(pos).count('1')
        if pos + next_moves <= N and pos + next_moves not in visited:
            queue.append((pos + next_moves, moves + 1))
        if pos - next_moves > 0 and pos - next_moves not in visited:
            queue.append((pos - next_moves, moves + 1))
    return -1
print(min_moves(int(input())))
0