結果

問題 No.3 ビットすごろく
ユーザー yuki2006
提出日時 2014-10-02 20:14:03
言語 Python2
(2.7.18)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 694 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-07-01 07:03:54
合計ジャッジ時間 2,332 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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))

0