結果

問題 No.3 ビットすごろく
ユーザー papinniepapinnie
提出日時 2019-09-19 17:47:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 617 bytes
コンパイル時間 115 ms
コンパイル使用メモリ 10,828 KB
実行使用メモリ 29,688 KB
最終ジャッジ日時 2023-09-14 01:28:34
合計ジャッジ時間 7,500 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
29,492 KB
testcase_01 AC 121 ms
29,556 KB
testcase_02 AC 118 ms
29,404 KB
testcase_03 AC 124 ms
29,528 KB
testcase_04 AC 119 ms
29,484 KB
testcase_05 AC 125 ms
29,528 KB
testcase_06 AC 120 ms
29,396 KB
testcase_07 AC 123 ms
29,300 KB
testcase_08 AC 125 ms
29,484 KB
testcase_09 AC 128 ms
29,548 KB
testcase_10 AC 137 ms
29,572 KB
testcase_11 AC 136 ms
29,456 KB
testcase_12 AC 129 ms
29,492 KB
testcase_13 AC 123 ms
29,472 KB
testcase_14 AC 134 ms
29,644 KB
testcase_15 AC 135 ms
29,564 KB
testcase_16 AC 130 ms
29,584 KB
testcase_17 AC 131 ms
29,568 KB
testcase_18 AC 122 ms
29,384 KB
testcase_19 AC 133 ms
29,588 KB
testcase_20 AC 121 ms
29,372 KB
testcase_21 AC 121 ms
29,464 KB
testcase_22 AC 129 ms
29,592 KB
testcase_23 AC 131 ms
29,688 KB
testcase_24 AC 132 ms
29,568 KB
testcase_25 AC 133 ms
29,676 KB
testcase_26 AC 120 ms
29,404 KB
testcase_27 AC 122 ms
29,484 KB
testcase_28 AC 129 ms
29,564 KB
testcase_29 AC 133 ms
29,564 KB
testcase_30 AC 120 ms
29,408 KB
testcase_31 AC 118 ms
29,412 KB
testcase_32 AC 126 ms
29,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import numpy as np

N = int(input())
moves = [bin(x).count('1') for x in range(N+1)]

routes = []

reachables = np.zeros(N+1, dtype=bool)
q = deque([(1,1)])
reachables[1] = True
while(q):
    n, c = q.popleft()
   
    if n == N:
        routes.append(c)
        break
    else:
        back, go =  moves[n]* -1 + n, moves[n]  + n
        for target in [go, back]:
            if (1 < target <= N) and not reachables[target]:
                reachables[target] = True
                q.append((target, c+1))

if len(routes) == 0:
    print(-1)  # unreachable
else:
    print(min(routes))
0