結果
問題 |
No.3 ビットすごろく
|
ユーザー |
|
提出日時 | 2017-03-09 00:16:11 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 741 bytes |
コンパイル時間 | 734 ms |
コンパイル使用メモリ | 75,176 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-07-01 08:35:17 |
合計ジャッジ時間 | 1,824 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include<iostream> #include<queue> using namespace std; const int INF = 10000000; int bitCount(int a){ int count = 0; while(a > 0){ if(a%2 != 0) count++; a = a/2; } return count; } int main(){ int n; cin >> n; vector<int> road(n+1, INF); road[1] = 1; queue<int> q; q.push(1); while(!q.empty()){ int now = q.front(); q.pop(); int mv = bitCount(now); //mainasu ni susumu case if(now - mv > 0){ if(road[now-mv] == INF) { //n kaisou road[now-mv] = road[now] + 1; q.push(now-mv); } } //purasu ni susumu case if(now + mv <= n){ if(road[now+mv] == INF){ road[now+mv] = road[now] + 1; q.push(now+mv); } } } if(road[n] == INF) cout << "-1" << endl; else cout << road[n] << endl; }