結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2017-06-01 19:18:22 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,095 bytes |
コンパイル時間 | 686 ms |
コンパイル使用メモリ | 71,868 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 21:41:02 |
合計ジャッジ時間 | 1,864 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 14 WA * 19 |
ソースコード
#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; stack<int> st; st.push(p); step[1] = 1; while (!st.empty()) { int np = st.top(); // cerr << np << endl; st.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); st.push(npp1); } npp1 = np - count(np); if (check(npp1)) { visited[npp1] = true; step[npp1] = min(step[npp1], step[np] + 1); st.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; }