結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2015-04-29 23:18:01 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 710 bytes |
コンパイル時間 | 541 ms |
コンパイル使用メモリ | 66,352 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-07-01 07:20:12 |
合計ジャッジ時間 | 1,469 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> #include <algorithm> #include <queue> using namespace std; #define MAX 10000 #define INF 1000000007 int N; int dist[MAX + 1]; void bfs(int start) { queue<int> q; dist[start] = 1; q.push(start); while (!q.empty()) { int x = q.front(); q.pop(); int cnt = __builtin_popcount(x); if (x + cnt <= N) { if (dist[x] + 1 < dist[x + cnt]) { dist[x + cnt] = dist[x] + 1; q.push(x + cnt); } } if (0 < x - cnt) { if (dist[x] + 1 < dist[x - cnt]) { dist[x - cnt] = dist[x] + 1; q.push(x - cnt); } } } } int main() { cin >> N; for (int i = 2; i <= N; i++) dist[i] = INF; bfs(1); cout << (dist[N] == INF ? -1 : dist[N]) << '\n'; return 0; }