結果

問題 No.3 ビットすごろく
ユーザー nanaenanae
提出日時 2016-12-28 07:14:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 842 ms / 5,000 ms
コード長 1,213 bytes
コンパイル時間 104 ms
コンパイル使用メモリ 10,908 KB
実行使用メモリ 8,560 KB
最終ジャッジ日時 2023-09-14 00:18:11
合計ジャッジ時間 11,970 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,800 KB
testcase_01 AC 18 ms
7,776 KB
testcase_02 AC 16 ms
7,960 KB
testcase_03 AC 65 ms
7,804 KB
testcase_04 AC 22 ms
7,900 KB
testcase_05 AC 249 ms
8,352 KB
testcase_06 AC 76 ms
7,760 KB
testcase_07 AC 36 ms
7,904 KB
testcase_08 AC 165 ms
8,312 KB
testcase_09 AC 405 ms
8,340 KB
testcase_10 AC 575 ms
8,492 KB
testcase_11 AC 345 ms
8,308 KB
testcase_12 AC 239 ms
8,368 KB
testcase_13 AC 51 ms
7,876 KB
testcase_14 AC 538 ms
8,532 KB
testcase_15 AC 820 ms
8,408 KB
testcase_16 AC 710 ms
8,560 KB
testcase_17 AC 794 ms
8,520 KB
testcase_18 AC 42 ms
7,808 KB
testcase_19 AC 834 ms
8,504 KB
testcase_20 AC 19 ms
7,804 KB
testcase_21 AC 16 ms
7,808 KB
testcase_22 AC 558 ms
8,384 KB
testcase_23 AC 842 ms
8,256 KB
testcase_24 AC 842 ms
8,504 KB
testcase_25 AC 822 ms
8,532 KB
testcase_26 AC 17 ms
7,872 KB
testcase_27 AC 59 ms
7,788 KB
testcase_28 AC 682 ms
8,252 KB
testcase_29 AC 354 ms
8,232 KB
testcase_30 AC 17 ms
7,804 KB
testcase_31 AC 16 ms
7,872 KB
testcase_32 AC 304 ms
8,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
def bit_sugoroku(N):
    if N == 1:
        return 1
    
    tansaku_zumi = [] # 探索済みの節点を格納するリスト
    tugi = [] # 次に調べる節点のリスト
    
    #初期化
    tansaku_zumi.append(1) # 1を探索済にする
    tugi.append(1) # 1を次に探索する節点に
    saitan = 1 # 節点Nへたどり着く最短距離
    
    while tugi: # 次に調べる節点が存在する限り続ける
        tugi_t = []
        saitan += 1
    
        for i in tugi:
            step = bin(i).count("1") # iを2進数にしたときの1のビット数
            
            if (i + step) == N or (i - step) == N:
                return saitan
            if (i - step) >= 1 and not((i - step) in tansaku_zumi): # i - step が境界を超えず、かつ未探索のとき
                tansaku_zumi.append(i - step)
                tugi_t.append(i - step)
            if (i + step) <= N and not((i + step) in tansaku_zumi): # i - step が境界を超えず、かつ未探索のとき
                tansaku_zumi.append(i + step)
                tugi_t.append(i + step)
    
        tugi = tugi_t[:]
    
    return -1

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