結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
JunOnuma
|
| 提出日時 | 2015-02-10 17:59:15 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 784 bytes |
| コンパイル時間 | 629 ms |
| コンパイル使用メモリ | 65,728 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 07:13:54 |
| 合計ジャッジ時間 | 1,500 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int countstep(int n)
{
int cnt = 0;
while(n != 0){
if(n % 2 == 1) cnt++;
n /= 2;
}
return cnt;
}
int main()
{
int n;
cin >> n;
vector<int> step(n+1,0);
for(int i=1; i<=n; i++) step[i] = countstep(i);
vector<int> route(n+1,-1);
route[1] = 1;
queue<int> pos;
pos.push(1);
while(!pos.empty()){
int p = pos.front(); pos.pop();
int pf = p + step[p];
if(pf <= n && (route[pf] == -1 || route[pf] > route[p] + 1)){
route[pf] = route[p] + 1;
pos.push(pf);
}
int pb = p - step[p];
if(pb > 0 && (route[pb] == -1 || route[pb] > route[p] + 1)){
route[pb] = route[p] + 1;
pos.push(pb);
}
}
cout << route[n] << endl;
return 0;
}
JunOnuma