結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,832 KB
testcase_01 AC 16 ms
7,808 KB
testcase_02 AC 16 ms
7,800 KB
testcase_03 AC 66 ms
8,248 KB
testcase_04 AC 21 ms
7,900 KB
testcase_05 AC 253 ms
8,348 KB
testcase_06 AC 78 ms
8,176 KB
testcase_07 AC 38 ms
7,836 KB
testcase_08 AC 167 ms
8,316 KB
testcase_09 AC 413 ms
8,532 KB
testcase_10 AC 584 ms
8,672 KB
testcase_11 AC 350 ms
8,472 KB
testcase_12 AC 242 ms
8,448 KB
testcase_13 AC 52 ms
7,900 KB
testcase_14 AC 545 ms
8,732 KB
testcase_15 AC 835 ms
8,792 KB
testcase_16 AC 721 ms
8,808 KB
testcase_17 AC 812 ms
8,756 KB
testcase_18 AC 43 ms
7,900 KB
testcase_19 AC 852 ms
8,624 KB
testcase_20 AC 19 ms
7,772 KB
testcase_21 AC 16 ms
7,884 KB
testcase_22 AC 561 ms
8,500 KB
testcase_23 AC 849 ms
8,668 KB
testcase_24 AC 848 ms
8,704 KB
testcase_25 AC 828 ms
8,708 KB
testcase_26 AC 16 ms
7,884 KB
testcase_27 AC 59 ms
7,824 KB
testcase_28 AC 699 ms
8,636 KB
testcase_29 AC 360 ms
8,584 KB
testcase_30 AC 16 ms
7,804 KB
testcase_31 AC 16 ms
7,876 KB
testcase_32 AC 309 ms
8,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
N = int(input())
saitan = [-1] * N # 各節点における最短移動数、-1は移動不可
tansaku_zumi = [] # 探索済みの節点を格納するリスト
tugi = [] # 次に調べる節点のリスト

#初期化
saitan[0] = 1 # 節点1の最短移動数は1
tansaku_zumi.append(1) # 1を探索済にする
tugi.append(1)

while tugi: # 次に調べる節点が無くなるまで続ける
    tugi_t = []
    
    for i in tugi:
        step = bin(i).count("1") # iを2進数にしたときの1のビット数
        
        if i - step >= 1 and not((i - step) in tansaku_zumi): # i - step が境界を超えず、かつ未探索のとき
            saitan[i - step - 1] = saitan[i - 1] + 1
            tansaku_zumi.append(i - step)
            tugi_t.append(i - step)
        if i + step <= N and not((i + step) in tansaku_zumi): # i - step が境界を超えず、かつ未探索のとき
            saitan[i + step - 1] = saitan[i - 1] + 1
            tansaku_zumi.append(i + step)
            tugi_t.append(i + step)
    
    tugi = tugi_t[:]
    #print(saitan)
    #print(tugi)
    #print(tansaku_zumi)
    
#print(saitan)
print(saitan[-1]) # 節点Nの最短移動数を出力
0