結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  mencotton | 
| 提出日時 | 2020-02-12 18:47:33 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3 ms / 5,000 ms | 
| コード長 | 861 bytes | 
| コンパイル時間 | 669 ms | 
| コンパイル使用メモリ | 76,104 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:38:58 | 
| 合計ジャッジ時間 | 1,628 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1145141919;
int bit_count(int x) {
    int ret = 0;
    while (x > 0) {
        if (x & 1)ret++;
        x >>= 1;
    }
    return ret;
}
int main() {
    int n;
    cin >> n;
    vector<int> minCost(n + 1, INF);
    minCost[1] = 1;
    queue<int> que;
    que.push(1);
    while (!que.empty()) {
        int here = que.front();
        que.pop();
        int move = bit_count(here);
        if (here + move <= n && minCost[here + move] == INF) {
            minCost[here + move] = minCost[here] + 1, que.push(here + move);
        }
        if (1 <= here - move && minCost[here - move] == INF) {
            minCost[here - move] = minCost[here] + 1, que.push(here - move);
        }
    }
    cout << (minCost[n] == INF ? -1 : minCost[n]) << endl;
    return 0;
}
            
            
            
        