結果

問題 No.3 ビットすごろく
ユーザー URechaRechaURechaRecha
提出日時 2019-08-22 11:40:56
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,563 ms / 5,000 ms
コード長 711 bytes
コンパイル時間 120 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-07-01 09:29:11
合計ジャッジ時間 20,723 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
def bitsugoroku(N,cur=1,path=None):
    h = []
    completed=[]
    heapq.heappush(h,[N,cur,[]])
    while len(h) > 0:
        h.sort(key=lambda x:len(x[2]))
        N,cur,path = heapq.heappop(h)
        if cur not in completed:
            path.append(cur)
            completed.append(cur)
            if N == cur:
                return len(path)
            a = str(bin(cur)).count("1")#移動する数
            if (cur + a <= N) and ((cur + a) not in completed):
                heapq.heappush(h,[N,cur+a,path.copy()])
            if (cur - a > 1) and ((cur -a) not in completed):
                heapq.heappush(h,[N,cur-a,path.copy()])
    return -1

N = int(input())
print(bitsugoroku(N))
0