結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2014-10-02 20:17:50 | 
| 言語 | Python2 (2.7.18) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 26 ms / 5,000 ms | 
| コード長 | 694 bytes | 
| コンパイル時間 | 44 ms | 
| コンパイル使用メモリ | 7,040 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 07:04:01 | 
| 合計ジャッジ時間 | 1,649 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
from collections import deque
def bitCount(n):
    count = 0
    while n > 0:
        n = n & (n - 1)
        count += 1
    return count
def bfs(N):
    table = [0] * (N + 1)
    q = deque()
    q.append(1)
    table[1] = 1
    while len(q) > 0:
        current = q.popleft()
        if current == N:
            return table[current]
        a = current + bitCount(current)
        b = current - bitCount(current)
        if 1 <= a <= N and table[a] == 0:
            q.append(a)
            table[a] = table[current] + 1
        if 1 <= b <= N and table[b] == 0:
            q.append(b)
            table[b] = table[current] + 1
    return -1
N = int(raw_input())
print (bfs(N))
            
            
            
        