結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-04-23 16:28:54 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3 ms / 5,000 ms | 
| コード長 | 630 bytes | 
| コンパイル時間 | 1,859 ms | 
| コンパイル使用メモリ | 171,444 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-06-25 03:37:07 | 
| 合計ジャッジ時間 | 2,912 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#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;
}
            
            
            
        