結果

問題 No.3 ビットすごろく
ユーザー maburoemaburoe
提出日時 2019-05-28 15:54:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 183 ms / 5,000 ms
コード長 1,117 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 10,932 KB
実行使用メモリ 9,372 KB
最終ジャッジ日時 2023-09-14 01:18:45
合計ジャッジ時間 4,239 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,876 KB
testcase_01 AC 16 ms
7,852 KB
testcase_02 AC 17 ms
7,876 KB
testcase_03 AC 35 ms
8,324 KB
testcase_04 AC 20 ms
8,140 KB
testcase_05 AC 78 ms
8,680 KB
testcase_06 AC 36 ms
8,384 KB
testcase_07 AC 26 ms
8,376 KB
testcase_08 AC 59 ms
8,712 KB
testcase_09 AC 106 ms
8,952 KB
testcase_10 AC 148 ms
9,112 KB
testcase_11 AC 94 ms
8,964 KB
testcase_12 AC 76 ms
8,780 KB
testcase_13 AC 31 ms
8,316 KB
testcase_14 AC 147 ms
9,148 KB
testcase_15 AC 181 ms
9,264 KB
testcase_16 AC 174 ms
9,180 KB
testcase_17 AC 176 ms
9,192 KB
testcase_18 AC 28 ms
8,384 KB
testcase_19 AC 181 ms
9,372 KB
testcase_20 AC 18 ms
8,320 KB
testcase_21 AC 16 ms
7,800 KB
testcase_22 AC 149 ms
9,064 KB
testcase_23 AC 182 ms
9,368 KB
testcase_24 AC 183 ms
9,320 KB
testcase_25 AC 179 ms
9,336 KB
testcase_26 AC 16 ms
7,880 KB
testcase_27 AC 34 ms
8,428 KB
testcase_28 AC 169 ms
9,104 KB
testcase_29 AC 98 ms
8,772 KB
testcase_30 AC 16 ms
7,888 KB
testcase_31 AC 17 ms
7,880 KB
testcase_32 AC 86 ms
8,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10000)

goal = int(input())

stepped = dict()
maybe_answer = -1


def step(position, count):
    global maybe_answer

    # 現在地点が範囲外
    if position not in range(1, goal + 1):
        return

    # 既に到達した位置か?
    if position in stepped:
        if count < stepped[position]:
            # 既出の到達経路より短い場合は、到達経路を更新
            stepped[position] = count
        else:
            # 既出の到達経路と同じ、または大きい場合は処理終了
            return
    else:
        stepped[position] = count

    # 既出のゴールまでの最短経路を超えてしまった
    if maybe_answer != -1 and count > maybe_answer:
        return

    # ゴールに到達したので処理終了
    if position == goal:
        maybe_answer = count
        return

    # 現在位置から進む・戻るを検証
    position_1bit_num = bin(position).count('1')
    step(position + position_1bit_num, count + 1)
    step(position - position_1bit_num, count + 1)


step(1, 1)

print(maybe_answer)
0