結果

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

ソースコード

diff #

from collections import deque

def bit_one_count(N):
    def maker(num):
        return sum([int(i) for i in list(bin(num)[2:])])
    return [maker(i) for i in range(1, N+1)]

def bfs(res, goal, route):
    Q = deque([])
    res[0] = 1
    goal -= 1
    Q.append(0)

    while Q:
        pos = Q.popleft()

        if pos == goal:
            return res

        for i in [route[pos], -route[pos]]:
            next_pos = pos + i

            if not 0 <= next_pos <= goal:
                continue

            if res[next_pos] == -1:
                Q.append(next_pos)
                res[next_pos] = res[pos] + 1
    return [-1]

def main():
    N = int(input())
    res = [-1] * N
    route = bit_one_count(N)
    s = bfs(res, N, route)
    print(s[-1])

if __name__ == '__main__':
    main()
0