結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  vrjfsyi | 
| 提出日時 | 2018-03-24 19:43:11 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,073 bytes | 
| コンパイル時間 | 579 ms | 
| コンパイル使用メモリ | 71,704 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:58:04 | 
| 合計ジャッジ時間 | 1,522 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#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;
    queue<int> q;
    q.push(1);
    int m[100005];
    for (int i = 1; i <= N; ++i) {
        m[i] = -1;
    }
    m[1] = 1;
    while (!q.empty()) {
        int x = q.front();
        q.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;
                q.push(array[i]);
            }
        }
    }
    cout << m[N] << "\n";
    return 0;
}
            
            
            
        