結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-06-26 10:42:22 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 40 ms / 5,000 ms | 
| コード長 | 637 bytes | 
| コンパイル時間 | 82 ms | 
| コンパイル使用メモリ | 12,416 KB | 
| 実行使用メモリ | 11,008 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:51:37 | 
| 合計ジャッジ時間 | 2,236 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
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())
            
            
            
        