結果

問題 No.3 ビットすごろく
ユーザー vrjfsyi
提出日時 2018-03-24 19:38:54
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,070 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 71,808 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-25 03:14:58
合計ジャッジ時間 2,093 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <array>
#include <map>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int countNumOfOneInBitRepresentation(int i) {

    int count = 0;
    while (i) {
        if (i & 1) {
            count += 1;
        }
        i = i >> 1;
    }
    return count;
}

int main() {

    int N;
    cin >> N;

    stack<int> s;
    s.push(1);

    int m[100005];
    for (int i = 1; i <= N; ++i) {
        m[i] = -1;
    }
    m[1] = 1;
    while (!s.empty()) {

        int x = s.top();
        s.pop();

        if (x == N) {
            break;
        }

        int numOfOneBit = countNumOfOneInBitRepresentation(x);

        int array[] = {x + numOfOneBit, x - numOfOneBit};

        for (int i = 0; i < 2; ++i) {
            if (1 <= array[i] and
                array[i] <= N and
                m[array[i]] == -1) {
                m[array[i]] = m[x] + 1;
                s.push(array[i]);
            }
        }
    }

    cout << m[N] << "\n";

    return 0;
}

0