結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  popketle | 
| 提出日時 | 2019-12-19 05:30:55 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 95 ms / 5,000 ms | 
| コード長 | 375 bytes | 
| コンパイル時間 | 94 ms | 
| コンパイル使用メモリ | 12,800 KB | 
| 実行使用メモリ | 11,392 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:36:18 | 
| 合計ジャッジ時間 | 3,156 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
from queue import Queue
n=int(input())
memo=[float('inf')]*n
def bfs(q):
  x,cnt=q.get()
  if x<1 or x>n:
    return
  else:
    if memo[x-1]>cnt:
      memo[x-1]=cnt
      dst=str(bin(x))[2:].count('1')
      q.put([x-dst,cnt+1])
      q.put([x+dst,cnt+1])
      
q=Queue()
q.put([1,1])
while not q.empty():
  bfs(q)
print(-1) if memo[-1]==float('inf') else print(memo[-1])
            
            
            
        