結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,972 KB
testcase_01 AC 16 ms
7,976 KB
testcase_02 AC 16 ms
7,872 KB
testcase_03 AC 72 ms
8,248 KB
testcase_04 AC 22 ms
7,804 KB
testcase_05 AC 277 ms
8,388 KB
testcase_06 AC 89 ms
8,304 KB
testcase_07 AC 40 ms
7,796 KB
testcase_08 AC 184 ms
8,480 KB
testcase_09 AC 452 ms
8,536 KB
testcase_10 AC 636 ms
8,676 KB
testcase_11 AC 382 ms
8,588 KB
testcase_12 AC 264 ms
8,428 KB
testcase_13 AC 57 ms
7,872 KB
testcase_14 AC 594 ms
8,752 KB
testcase_15 AC 908 ms
8,752 KB
testcase_16 AC 787 ms
8,636 KB
testcase_17 AC 882 ms
8,720 KB
testcase_18 AC 47 ms
7,828 KB
testcase_19 AC 927 ms
8,804 KB
testcase_20 AC 19 ms
7,868 KB
testcase_21 AC 15 ms
7,868 KB
testcase_22 AC 612 ms
8,612 KB
testcase_23 AC 931 ms
8,664 KB
testcase_24 AC 929 ms
8,676 KB
testcase_25 AC 904 ms
8,804 KB
testcase_26 AC 16 ms
7,900 KB
testcase_27 AC 65 ms
7,884 KB
testcase_28 AC 756 ms
8,784 KB
testcase_29 AC 393 ms
8,616 KB
testcase_30 AC 16 ms
7,792 KB
testcase_31 AC 16 ms
7,968 KB
testcase_32 AC 338 ms
8,504 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 and not N in tansaku_zumi: # 次に調べる節点が存在、かつ節点Nが探索済みで無いとき継続
    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