結果

問題 No.3 ビットすごろく
ユーザー H3PO4
提出日時 2023-02-10 17:41:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,001 bytes
コンパイル時間 973 ms
コンパイル使用メモリ 83,068 KB
最終ジャッジ日時 2025-02-10 11:55:31
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <bitset>

int main() {
    int N;
    std::cin >> N;

    std::vector<bool> non_visited(N + 1, true);
    const int INF = 10001;
    std::vector<int> cnt(N + 1, INF);
    std::vector<int> next_dist(N + 1);
    for (int i = 1; i < N + 1; i++) {
        std::bitset<32> bs(i);
        next_dist.at(i) = bs.count();
    }
    std::queue<int> q;
    q.push(1);
    non_visited.at(1) = false;
    cnt.at(1) = 1;
    while (!q.empty()) {
        int x = q.front();
        if (x == N) {
            break;
        }
        q.pop();
        for (auto &y: {x + next_dist.at(x), x - next_dist.at(x)}) {
            if (y <= 0 || N < y) {
                continue;
            }
            if (non_visited.at(y)) {
                q.push(y);
                non_visited.at(y) = false;
                cnt.at(y) = cnt.at(x) + 1;
            }
        }
    }
    int ans = (cnt.at(N) != INF) ? cnt.at(N) : -1;
    std::cout << ans << std::endl;
}
0