結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
hanorver
|
| 提出日時 | 2016-05-18 09:54:01 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 810 bytes |
| コンパイル時間 | 636 ms |
| コンパイル使用メモリ | 68,716 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 07:53:27 |
| 合計ジャッジ時間 | 1,548 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
int bit_num(int num) {
int count = 0;
while (num != 0) {
if (num % 2 == 1) count++;
num /= 2;
}
return count;
}
int main() {
int n;
std::cin >> n;
const int INF = 9999999;
std::vector<int> masu(n + 1);
for (int i = 0; i < masu.size(); i++) masu[i] = INF;
masu[1] = 1;
std::queue<int> s;
s.push(1);
while(!s.empty()) {
int pos = s.front();
s.pop();
int bit = bit_num(pos);
if (pos + bit < masu.size() && masu[pos + bit] > masu[pos] + 1) {
masu[pos + bit] = masu[pos] + 1;
s.push(pos + bit);
}
if (pos - bit > 0 && masu[pos - bit] > masu[pos] + 1){
masu[pos - bit] = masu[pos] + 1;
s.push(pos - bit);
}
}
if (masu[n] == INF) masu[n] = -1;
std::cout << masu[n] << std::endl;
return 0;
}
hanorver