結果

問題 No.3 ビットすごろく
ユーザー mahiromahiro
提出日時 2020-01-11 19:41:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 187 ms / 5,000 ms
コード長 682 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 78,476 KB
最終ジャッジ日時 2023-09-14 01:34:16
合計ジャッジ時間 6,118 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,476 KB
testcase_01 AC 80 ms
71,068 KB
testcase_02 AC 80 ms
71,388 KB
testcase_03 AC 118 ms
77,676 KB
testcase_04 AC 84 ms
71,200 KB
testcase_05 AC 152 ms
77,956 KB
testcase_06 AC 118 ms
77,732 KB
testcase_07 AC 99 ms
76,460 KB
testcase_08 AC 142 ms
77,912 KB
testcase_09 AC 166 ms
78,380 KB
testcase_10 AC 178 ms
78,476 KB
testcase_11 AC 168 ms
78,292 KB
testcase_12 AC 150 ms
77,920 KB
testcase_13 AC 113 ms
77,740 KB
testcase_14 AC 172 ms
78,344 KB
testcase_15 AC 186 ms
78,352 KB
testcase_16 AC 184 ms
78,356 KB
testcase_17 AC 186 ms
78,328 KB
testcase_18 AC 101 ms
76,604 KB
testcase_19 AC 185 ms
78,468 KB
testcase_20 AC 83 ms
71,420 KB
testcase_21 AC 80 ms
71,080 KB
testcase_22 AC 171 ms
78,292 KB
testcase_23 AC 186 ms
78,472 KB
testcase_24 AC 187 ms
78,296 KB
testcase_25 AC 187 ms
78,056 KB
testcase_26 AC 78 ms
71,380 KB
testcase_27 AC 112 ms
77,736 KB
testcase_28 AC 177 ms
78,296 KB
testcase_29 AC 162 ms
78,448 KB
testcase_30 AC 80 ms
71,204 KB
testcase_31 AC 79 ms
71,044 KB
testcase_32 AC 155 ms
78,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


N = int(input())
visited = [False] * (N + 1)
visited[1] = True
queue = [(1, 1)]  # (訪問するまでにかかった移動数, 現在のマス)

ans = -1
while queue:
    q = heapq.heappop(queue)
    moved, now_space = q
    if now_space == N:
        ans = moved
        break
    next_move = bin(now_space).count("1")
    forward, backward = now_space + next_move, now_space - next_move
    if forward < N + 1 and not visited[forward]:
        visited[forward] = True
        heapq.heappush(queue, (moved + 1, forward))
    if backward > 0 and not visited[backward]:
        visited[backward] = True
        heapq.heappush(queue, (moved + 1, backward))
print(ans)
0