結果

問題 No.3 ビットすごろく
ユーザー szkhtsszkhts
提出日時 2020-06-26 10:42:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 637 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 10,836 KB
実行使用メモリ 8,924 KB
最終ジャッジ日時 2023-09-14 01:47:06
合計ジャッジ時間 2,179 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,476 KB
testcase_01 AC 21 ms
8,636 KB
testcase_02 AC 21 ms
8,568 KB
testcase_03 AC 20 ms
8,560 KB
testcase_04 AC 19 ms
8,636 KB
testcase_05 AC 22 ms
8,612 KB
testcase_06 AC 21 ms
8,560 KB
testcase_07 AC 20 ms
8,588 KB
testcase_08 AC 22 ms
8,568 KB
testcase_09 AC 25 ms
8,724 KB
testcase_10 AC 26 ms
8,788 KB
testcase_11 AC 25 ms
8,756 KB
testcase_12 AC 23 ms
8,640 KB
testcase_13 AC 20 ms
8,476 KB
testcase_14 AC 26 ms
8,872 KB
testcase_15 AC 28 ms
8,904 KB
testcase_16 AC 28 ms
8,776 KB
testcase_17 AC 28 ms
8,904 KB
testcase_18 AC 20 ms
8,524 KB
testcase_19 AC 28 ms
8,848 KB
testcase_20 AC 19 ms
8,504 KB
testcase_21 AC 19 ms
8,420 KB
testcase_22 AC 26 ms
8,788 KB
testcase_23 AC 28 ms
8,924 KB
testcase_24 AC 28 ms
8,780 KB
testcase_25 AC 28 ms
8,864 KB
testcase_26 AC 18 ms
8,500 KB
testcase_27 AC 20 ms
8,456 KB
testcase_28 AC 27 ms
8,800 KB
testcase_29 AC 25 ms
8,760 KB
testcase_30 AC 19 ms
8,612 KB
testcase_31 AC 19 ms
8,468 KB
testcase_32 AC 25 ms
8,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N = int(input())

def bfs():
    bit_route = deque([0] * (N + 1))
    bit_route[1] = 1
    que = deque()
    que.append(1)

    while(que):
        x = que.popleft()
        if x == N:
            return bit_route[x]
        
        bit_count = int(bin(x).count('1'))
        a = x + bit_count
        b = x - bit_count
        
        if (0 < a <= N) and (bit_route[a] == 0):
            que.append(a)
            bit_route[a] = bit_route[x] + 1
        if (0 < b <= N) and (bit_route[b] == 0):
            que.append(b)
            bit_route[b] = bit_route[x] + 1
      
    return -1
    
print(bfs())
0