結果

問題 No.3 ビットすごろく
ユーザー cocomoff
提出日時 2019-08-07 20:10:40
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 949 bytes
コンパイル時間 1,584 ms
コンパイル使用メモリ 165,212 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-18 18:50:29
合計ジャッジ時間 2,228 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 

using namespace std;
typedef long long ll;

int bitCount(int bits) {
    int cnt = 0;
    for(int mask = 1; mask != 0; mask <<= 1) {
        if( (bits & mask) != 0 )
            ++cnt;
    }
    return cnt;
}

int main() {
    int N;
    cin >> N;
    priority_queue<pair<int,int>> P;
    P.push(make_pair(1, 1));
    vector<bool> used(N + 1, false);

    int fc = -1;
    while (!P.empty()) {
        pair<int,int> q = P.top(); P.pop();
        int c = q.first;
        if (c == N) {
            cout << q.second << endl;
            return 0;
        }
        int d = bitCount(c);
        int l = c - d;
        if (1 <= l && l <= N && !used[l]) {
            used[l] = true;
            P.push(make_pair(l, q.second + 1));
        }
        int r = c + d;
        if (1 <= r && r <= N && !used[r]) {
            used[r] = true;
            P.push(make_pair(r, q.second + 1));
        }
    }
    cout << -1 << endl;
}
0