結果

問題 No.3 ビットすごろく
ユーザー bonyuta0204bonyuta0204
提出日時 2017-04-14 12:46:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,124 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 486,304 KB
最終ジャッジ日時 2023-09-25 19:17:20
合計ジャッジ時間 7,458 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,676 KB
testcase_01 AC 19 ms
8,672 KB
testcase_02 AC 19 ms
8,668 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N = int(input())
num_dict = {}
def _d_to_one(digit):
    """
    return number of 1 in binary of digit
    """
    binary = "{0: b}".format(digit)
    num_one = 0
    for i in range(len(binary)):
        if binary[i] == "1":
            num_one += 1
    return num_one

def d_to_one(digit):
    try:
        num = num_dict[digit]
    except KeyError:
        num = _d_to_one(digit)
        num_dict[digit] = num
    return num
def next_num(digits):
    step = d_to_one(digits)
    next = []
    if digits - step > 1:
        next.append(digits - step)
        
    if digits + step <= N:
        next.append(digits + step)
    return next


def bfs(start):
    if start == N:
        return [start]
    queue = deque([(start, [start])])
    
    while queue:
        vertex, path = queue.popleft()
        for next in next_num(vertex):
            if next == N:
                path.append(next)
                return path
            else:
                queue.append((next, path + [next]))
    return None

result = bfs(1)
if result is not None:
    print(len(result))
else:
    print(-1)
0