結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-01 17:46:38 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 35 ms / 5,000 ms |
| コード長 | 816 bytes |
| コンパイル時間 | 86 ms |
| コンパイル使用メモリ | 12,544 KB |
| 実行使用メモリ | 11,136 KB |
| 最終ジャッジ日時 | 2024-09-30 21:55:45 |
| 合計ジャッジ時間 | 1,929 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
from collections import deque
def count_bits(n):
return bin(n).count("1")
def bfs_min_moves(N):
if N == 1:
return 1
moves = [float('inf')] * (N + 1)
moves[1] = 1
queue = deque([1])
while queue:
current = queue.popleft()
step = count_bits(current)
if current + step <= N and moves[current + step] == float('inf'):
moves[current + step] = moves[current] + 1
queue.append(current + step)
if current - step >= 1 and moves[current - step] == float('inf'):
moves[current - step] = moves[current] + 1
queue.append(current - step)
if moves[N] != float('inf'):
return moves[N]
return -1
def main():
print(bfs_min_moves(int(input())))
if __name__ == "__main__":
main()