結果

問題 No.3 ビットすごろく
ユーザー mh72012246
提出日時 2016-10-18 17:11:50
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,123 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 75,376 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 08:07:17
合計ジャッジ時間 1,620 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <sstream>   // istringstream
#include <algorithm> // sort
#include <utility>   // pair
#include <cfloat>    // DBL_MAX

typedef long long ll;

using namespace std;

int bitCount(int);

int main(){
  //// input
  int N;
  cin >> N;

  //// main
  queue<pair<int,int> > search;
  vector<bool> checked(N, false);
  search.push(make_pair(1,1));
  checked[1-1] = true;

  while(true){
    if(search.empty()){
      cout << -1 << endl;
      break;
    }

    pair<int,int> crr = search.front();
    search.pop();

    if(crr.first == N){
      cout << crr.second << endl;
      break;
    }

    int bc = bitCount(crr.first);
    int t1 = crr.first + bc;
    int t2 = crr.first - bc;

    if(t1 <= N && !checked[t1-1]){
      search.push(make_pair(t1, crr.second+1));
      checked[t1-1] = true;
    }
    if(t2 > 0 && !checked[t2-1]){
      search.push(make_pair(t2, crr.second+1));
      checked[t2-1] = true;
    }
  }

  return 0;
}

int bitCount(int n){
  int count=0;
  while(n>0){
    if(n&1){ count++; }
    n >>= 1;
  }
  return count;
}
0