結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2017-02-09 13:29:40 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 825 bytes |
コンパイル時間 | 626 ms |
コンパイル使用メモリ | 75,008 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-01 08:17:20 |
合計ジャッジ時間 | 1,554 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> #include <queue> using namespace std; const int INF = 100000000; int main(void){ int n, i; int v[10005], d[10005]; cin >> n; for(int i=1;i<=n;i++){ v[i] = 0; } for(int i=1;i<=n;i++){ d[i] = INF; } queue<int> que; que.push(1); v[1] = 1; d[1] = 1; while(!que.empty()){ int bit; i = que.front(); que.pop(); bit = __builtin_popcount(i); if(i+bit >= 1 && i+bit <= n && v[i+bit] == 0){ que.push(i+bit); v[i+bit] = 1; d[i+bit] = d[i] + 1; } if(i-bit >= 1 && i-bit <= n && v[i-bit] == 0){ que.push(i-bit); v[i-bit] = 1; d[i-bit] = d[i] + 1; } } cout << (d[n] != INF ? d[n] : -1) << endl; return 0; }