結果

問題 No.3 ビットすごろく
ユーザー satanic
提出日時 2015-11-26 02:38:37
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 824 bytes
コンパイル時間 628 ms
コンパイル使用メモリ 66,072 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-13 17:39:56
合計ジャッジ時間 1,871 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

int fnNumOneInBit(int n){
  int result=0;
  while(n>0){
    if(n%2==1) ++result;
    n/=2;
  }
  return result;
}

int main(){
  int tmp;
  int now, d;
  queue<int> Queue;
  cin >> tmp;  //入力
  const int n=tmp;
  int depth[n]={0};
  bool bLoop=true;
  int depthMin;

  //幅優先探索
  Queue.push(1);
  depth[0]=1;

  while(!Queue.empty()){
    now=Queue.front();
    Queue.pop();

    if(now==n){
      bLoop=false;
      break;
    }else{
      d=fnNumOneInBit(now);
      for(int i=0; i<2; ++i){
	tmp = (i==0) ? (now-d) : (now+d);
	if(tmp>0 && tmp<=n && depth[tmp-1]==0){
	  Queue.push(tmp);
	  depth[tmp-1]=depth[now-1]+1;
	}
      }
    }
  }

  if(bLoop) depthMin=-1;
  else      depthMin=depth[n-1];
  cout << depthMin << endl; //出力

  return 0;
}
0