結果

問題 No.3 ビットすごろく
ユーザー Mario
提出日時 2018-05-19 13:47:32
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 979 ms / 5,000 ms
コード長 749 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-07-01 09:01:39
合計ジャッジ時間 13,568 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def count_1(n):
    return list((bin(n)))[2:].count("1")

class BiSugoroku():
    def __init__(self, n, step):
        self.n = n
        st = count_1(n)
        self.plus = self.n + st
        self.minus = self.n - st
        self.step = step


n = int(input())

q = deque([BiSugoroku(1, 1)])
visited = [1]
step = -1

while len(q) > 0:
    b = q.popleft()

    if  b.n == n:
        step = b.step
        break
    else:
        if b.plus <= n and not (b.plus in visited):
            q.append(BiSugoroku(b.plus, b.step + 1))
            visited.append(b.plus)

        if 0 < b.minus and not (b.minus in visited):
            q.append(BiSugoroku(b.minus, b.step + 1))
            visited.append(b.minus)

print(step)
0