結果

問題 No.3 ビットすごろく
ユーザー Rumain831
提出日時 2025-05-03 00:14:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 583 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 79,424 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2025-05-03 00:14:11
合計ジャッジ時間 2,207 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int main(void){
  int n; cin >> n;
  vector<int> ans(n+1, 1e8);
  queue<pair<int, int>> bfs;
  bfs.emplace(1, 0);
  while(bfs.size()){
    auto [idx, cnt]=bfs.front(); bfs.pop();
    if(ans[idx]!=1e8) continue;
    if(idx==n){
      cout << cnt+1 << endl; return 0;
    }
    ans[idx]=cnt;
    int copy=idx;
    int k=0;
    while(copy>0){
      if(copy%2) k++;
      copy/=2;
    }
    if(idx+k<=n) bfs.emplace(idx+k, cnt+1);
    if(idx-k>0) bfs.emplace(idx-k, cnt+1);
  }
  cout << -1 << endl;
  return 0;
}
0