結果

問題 No.3 ビットすごろく
ユーザー MShinyaMShinya
提出日時 2018-10-02 08:18:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 795 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 9,008 KB
最終ジャッジ日時 2023-09-14 01:07:53
合計ジャッジ時間 2,533 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,508 KB
testcase_01 AC 19 ms
8,508 KB
testcase_02 AC 19 ms
8,552 KB
testcase_03 AC 26 ms
8,652 KB
testcase_04 AC 21 ms
8,556 KB
testcase_05 AC 36 ms
8,680 KB
testcase_06 AC 27 ms
8,616 KB
testcase_07 AC 23 ms
8,624 KB
testcase_08 AC 32 ms
8,796 KB
testcase_09 AC 42 ms
8,736 KB
testcase_10 AC 46 ms
8,960 KB
testcase_11 AC 43 ms
8,740 KB
testcase_12 AC 36 ms
8,728 KB
testcase_13 AC 24 ms
8,564 KB
testcase_14 AC 46 ms
8,824 KB
testcase_15 AC 52 ms
8,880 KB
testcase_16 AC 47 ms
8,832 KB
testcase_17 AC 51 ms
8,868 KB
testcase_18 AC 24 ms
8,624 KB
testcase_19 AC 51 ms
8,956 KB
testcase_20 AC 20 ms
8,640 KB
testcase_21 AC 19 ms
8,608 KB
testcase_22 AC 46 ms
8,964 KB
testcase_23 AC 53 ms
8,860 KB
testcase_24 AC 52 ms
8,984 KB
testcase_25 AC 52 ms
8,924 KB
testcase_26 AC 19 ms
8,668 KB
testcase_27 AC 24 ms
8,644 KB
testcase_28 AC 49 ms
9,008 KB
testcase_29 AC 39 ms
8,708 KB
testcase_30 AC 19 ms
8,476 KB
testcase_31 AC 19 ms
8,552 KB
testcase_32 AC 37 ms
8,880 KB
権限があれば一括ダウンロードができます

ソースコード

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