結果

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

ソースコード

diff #

from collections import deque


def bfs(g: int) -> int:
    queue = deque()
    queue.appendleft((1, 1))
    done = [-1] * (g + 1)
    done[1] = 0
    ans = -1
    while queue:
        d = queue.pop()
        if d[0] == g:
            return d[1]
        cnt = 0
        for i in range(16):
            if (d[0] >> i) & 1:
                cnt += 1
        for a in [1, -1]:
            x = d[0] + cnt * a
            if 0 < x <= g and done[x] == -1:
                queue.appendleft((x, d[1] + 1))
                done[x] = d[1] + 1
    return -1


n = int(input())
print(bfs(n))
0