結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
Naoyk1212
|
| 提出日時 | 2017-06-02 14:07:33 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 1,090 bytes |
| コンパイル時間 | 845 ms |
| コンパイル使用メモリ | 72,260 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 08:44:06 |
| 合計ジャッジ時間 | 2,160 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int n;
bool visited[100010];
int step[100010];
bool check(int p) {
bool ok = true;
if (visited[p])ok = false;
if (p < 1)ok = false;
if (p > n)ok = false;
return ok;
}
int count(int a) {
int sum = 0;
for (int i = 0; (a >> i) > 0; i++) {
sum += ((a >> i) & 1);
}
return sum;
}
bool solve(int p) {
bool ok = false;
queue<int> q;
q.push(p);
step[1] = 1;
while (!q.empty()) {
int np = q.front();
// cerr << np << endl;
q.pop();
if (np == n)ok = true;
int npp1 = np + count(np);
if (check(npp1)) {
visited[npp1] = true;
step[npp1] = min(step[npp1], step[np] + 1);
q.push(npp1);
}
npp1 = np - count(np);
if (check(npp1)) {
visited[npp1] = true;
step[npp1] = min(step[npp1], step[np] + 1);
q.push(npp1);
}
}
return ok;
}
int main() {
cin >> n;
for (int i = 0; i < 1e5 + 10; i++) {
visited[i] = false;
step[i] = 1e9;
}
cout << (solve(1) ? step[n] : -1) << endl;
}
Naoyk1212