結果

問題 No.3 ビットすごろく
ユーザー LunaLuna
提出日時 2018-06-09 14:38:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 1,211 bytes
コンパイル時間 1,916 ms
コンパイル使用メモリ 69,388 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 01:01:54
合計ジャッジ時間 2,602 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 16 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 11 ms
4,376 KB
testcase_09 AC 26 ms
4,380 KB
testcase_10 AC 37 ms
4,376 KB
testcase_11 AC 22 ms
4,376 KB
testcase_12 AC 15 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 35 ms
4,380 KB
testcase_15 AC 52 ms
4,380 KB
testcase_16 AC 46 ms
4,376 KB
testcase_17 AC 52 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 55 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 35 ms
4,376 KB
testcase_23 AC 55 ms
4,376 KB
testcase_24 AC 55 ms
4,380 KB
testcase_25 AC 53 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 44 ms
4,376 KB
testcase_29 AC 23 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 20 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <vector>
#include <queue>

using namespace std;

int count(int n) {
    int cnt = 0;
    while (n > 0) {
        if (n % 2 == 1) cnt++;
        n = n / 2;
    }
    return cnt;
}

bool in(vector<int> v, int n) {
    for (auto var : v) {
        if (var == n) return true;
    }
    return false;
}

int solve(int n) {
    queue<pair<int, int>> q;
    q.push(make_pair(1, 1));
    vector<int> v;
    v.push_back(1);
    while (!q.empty()) {
        pair<int, int> tmp = q.front();
        int num = tmp.first;
        int cnt = tmp.second;
        q.pop();
        if (num == n) return cnt;
        int forward  = num + count(num);
        int backward = num - count(num);
        if (forward <= n) {
            if (!in(v, forward)) {
                q.push(make_pair(forward, cnt+1));
                v.push_back(forward);
            }
        }
        if (backward > 0) {
            if (!in(v, backward)) {
                q.push(make_pair(backward, cnt+1));
                v.push_back(backward);
            }
        }
    }
    return -1;
}

int main(int argc, char const* argv[]) {
    int n;
    cin >> n;
    cout << solve(n) << endl;
    return 0;
}
0