結果

問題 No.3 ビットすごろく
ユーザー eiji0313eiji0313
提出日時 2018-01-08 01:18:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 985 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 10,872 KB
実行使用メモリ 191,748 KB
最終ジャッジ日時 2023-08-24 23:47:27
合計ジャッジ時間 11,118 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,672 KB
testcase_01 AC 18 ms
8,416 KB
testcase_02 AC 19 ms
8,480 KB
testcase_03 AC 73 ms
11,136 KB
testcase_04 AC 24 ms
8,992 KB
testcase_05 AC 2,009 ms
64,288 KB
testcase_06 AC 79 ms
11,308 KB
testcase_07 AC 40 ms
9,524 KB
testcase_08 AC 519 ms
26,460 KB
testcase_09 TLE -
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


def bfs(start, goal):
    queue = deque([])
    visited = set()
    queue.append([start])

    while queue:
        # print("="*30)
        # print(queue)
        v = queue.popleft()

        # print("{} checking...".format(v[-1]))

        if v[-1] == goal:
            return v
            exit()

        for x in g[v[-1]]:
            if x in visited:
                # print("{} is already visited.".format(x))
                pass
            if x not in visited:
                # print("{} is hajimete.".format(x))
                # print(v + [x])
                queue.append(v + [x])
                visited.add(v[-1])


N = int(input())
m = []
g = {}
for i in range(1, N + 1):
    m.append("{:b}".format(i).count("1"))
for i in range(len(m)):
    l = []
    if i + m[i] < len(m):
        l.append(i + m[i])
    if i - m[i] >= 0:
        l.append(i - m[i])
    g[i] = l

path = bfs(0, N - 1)
if path:
    print(len(path))
else:
    print("-1")
0