結果
問題 |
No.3 ビットすごろく
|
ユーザー |
|
提出日時 | 2015-11-26 02:39:56 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 848 bytes |
コンパイル時間 | 555 ms |
コンパイル使用メモリ | 66,672 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-01 07:37:05 |
合計ジャッジ時間 | 1,523 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> #include <queue> #include <vector> 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; vector<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; }