結果

問題 No.3 ビットすごろく
コンテスト
ユーザー Aurora
提出日時 2022-04-23 16:28:54
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 630 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,334 ms
コンパイル使用メモリ 190,184 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-11 08:18:22
合計ジャッジ時間 2,346 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;

int main(){
  int N;
  cin >> N;
  int INF = 1000000000;
  vector<int> dist(N+1,INF);
  dist[1] = 1;
  queue<int> que;
  que.push(1);
  while(!que.empty()){
    int v = que.front();
    que.pop();
    int cnt = 0;
    for(int i=0;i<20;i++) if(v & (1<<i)) cnt++;
    if(v-cnt >= 1){
      if(dist[v-cnt] == INF){
        dist[v-cnt] = dist[v]+1;
        que.push(v-cnt);
      }
    }
    if(v+cnt <= N){
      if(dist[v+cnt] == INF){
        dist[v+cnt] = dist[v]+1;
        que.push(v+cnt);
      }
    }
  }
  if(dist[N] == INF) cout << -1 << endl;
  else cout << dist[N] << endl;
}
0