結果

問題 No.3 ビットすごろく
ユーザー mkanmkan
提出日時 2017-03-26 13:48:09
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 809 bytes
コンパイル時間 686 ms
コンパイル使用メモリ 73,068 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 08:37:34
合計ジャッジ時間 1,621 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cctype>
#define rep(i,a,b) for(int i=(a);i<b;i++)
#define INF 1000000000
using namespace std;
bool debug=true;
int n;

int change(int n){
  int cnt=0;
  while(n){
    if(n%2==1)cnt++;
    n/=2;
  }
  return cnt;
}

void bfs(){
  vector<int> vect(n+1,INF);
  queue<int>que;
  que.push(1);
  vect[1]=1;
  while(que.size()){
    int now=que.front();que.pop();
    int bit=change(now);
    if(now-bit>0&&vect[now-bit]==INF){
      vect[now-bit]=vect[now]+1;
      que.push(now-bit);
    }
    if(now+bit<=n&&vect[now+bit]==INF){
      vect[now+bit]=vect[now]+1;
      que.push(now+bit);
    }
  }
  cout<<( vect[n]!=INF ? vect[n] : -1 ) <<endl;
}

int main(){
  cin>>n;
  bfs();
  return 0;
}
0