結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-10-12 15:14:43 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 533 ms / 5,000 ms | 
| コード長 | 883 bytes | 
| コンパイル時間 | 739 ms | 
| コンパイル使用メモリ | 77,500 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:57:47 | 
| 合計ジャッジ時間 | 6,068 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include<iostream>
#include<queue>
#define ll long long
using namespace std;
typedef pair<int,int> P;
int bitcnt(int n){
  int bit = 0;
  while(n > 0){
    if(n & 1){
      bit++;
    }
    n >>= 1;
  }
  return bit;
}
int main(){
    int n;
    cin >> n;
    queue<P> que;
    que.push(P(1,1));
    vector<bool> used(n+1,false);
    
    while(!que.empty()){
      int now = que.front().first;
      int cost = que.front().second;      
      que.pop();
      
      used[now] = true;
      
      if(now == n){
        cout << cost << endl;
        return 0;
      }
      
      int next = now + bitcnt(now);
      int prev = now - bitcnt(now);
      
      if(next > 0 && next <= n && !used[next]){
        que.push(P(next,cost+1));
      }
      if(prev > 0 && prev <= n && !used[prev]){
        que.push(P(prev,cost+1));
      }
    }
    cout << -1 << endl;
    return 0;
}
            
            
            
        