結果

問題 No.3 ビットすごろく
ユーザー URechaRechaURechaRecha
提出日時 2019-08-22 11:40:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,366 ms / 5,000 ms
コード長 711 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 10,904 KB
実行使用メモリ 9,036 KB
最終ジャッジ日時 2023-09-14 01:26:39
合計ジャッジ時間 18,495 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,984 KB
testcase_01 AC 17 ms
7,964 KB
testcase_02 AC 17 ms
8,012 KB
testcase_03 AC 100 ms
8,496 KB
testcase_04 AC 26 ms
8,120 KB
testcase_05 AC 410 ms
8,508 KB
testcase_06 AC 118 ms
8,512 KB
testcase_07 AC 52 ms
8,044 KB
testcase_08 AC 262 ms
8,460 KB
testcase_09 AC 657 ms
8,724 KB
testcase_10 AC 933 ms
8,816 KB
testcase_11 AC 554 ms
8,796 KB
testcase_12 AC 382 ms
8,352 KB
testcase_13 AC 76 ms
8,496 KB
testcase_14 AC 877 ms
8,896 KB
testcase_15 AC 1,332 ms
8,868 KB
testcase_16 AC 1,157 ms
8,920 KB
testcase_17 AC 1,292 ms
8,828 KB
testcase_18 AC 62 ms
8,368 KB
testcase_19 AC 1,366 ms
8,956 KB
testcase_20 AC 21 ms
8,020 KB
testcase_21 AC 17 ms
8,008 KB
testcase_22 AC 896 ms
8,924 KB
testcase_23 AC 1,363 ms
8,900 KB
testcase_24 AC 1,365 ms
9,036 KB
testcase_25 AC 1,332 ms
8,796 KB
testcase_26 AC 16 ms
8,072 KB
testcase_27 AC 88 ms
8,456 KB
testcase_28 AC 1,106 ms
8,820 KB
testcase_29 AC 571 ms
8,760 KB
testcase_30 AC 17 ms
8,064 KB
testcase_31 AC 17 ms
7,960 KB
testcase_32 AC 488 ms
8,616 KB
権限があれば一括ダウンロードができます

ソースコード

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