結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-06-29 15:59:40 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,028 bytes |
| コンパイル時間 | 538 ms |
| コンパイル使用メモリ | 65,480 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 08:45:13 |
| 合計ジャッジ時間 | 1,526 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using uint = unsigned int;
int bitCount(uint bits) {
int count = 0;
for (uint mask = 1; mask <= bits; mask<<=1) {
if ((bits & mask) != 0) ++count;
}
return count;
}
int main() {
uint n; cin >> n;
queue<uint> positions; positions.push(1);
vector<int> move_n(n + 1, 0); move_n[1] = 1;
while (!positions.empty()) {
uint pos = positions.front(); positions.pop();
if (pos == n) {
cout << move_n[pos] << endl;
return 0;
}
int bit_cnt = bitCount(pos);
int new_pos0 = pos + bit_cnt;
int new_pos1 = pos - bit_cnt;
if (new_pos0 <= n && move_n[new_pos0] == 0) {
positions.push(new_pos0);
move_n[new_pos0] = move_n[pos] + 1;
}
if (new_pos1 > 0 && move_n[new_pos1] == 0) {
positions.push(new_pos1);
move_n[new_pos1] = move_n[pos] + 1;
}
}
cout << -1 << endl;
}