結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-04-16 16:54:42 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,399 bytes | 
| コンパイル時間 | 578 ms | 
| コンパイル使用メモリ | 66,528 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 07:49:08 | 
| 合計ジャッジ時間 | 1,534 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int func(int a) {
    a = (a & 0x5555) + ((a >> 1) & 0x5555);
    a = (a & 0x3333) + ((a >> 2) & 0x3333);
    a = (a & 0x0F0F) + ((a >> 4) & 0x0F0F);
    a = (a & 0x00FF) + ((a >> 8) & 0x00FF);
    return a;
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    cin >> n;
    //at(i)が0なら未訪問,その他のときは既に来た
    //ゴールまでの移動数....辿れた道のとき、直前のノードまでの値を足す
    //↑38行目と44行目
    vector<int> vec(n + 1, 0);
    vec.at(1) = 1;
    queue<int> q;
    q.push(1);
    while (!q.empty()) {
        int position = q.front();
        q.pop();
        int step = func(position);
        //戻るとき
        if (position - step > 0 && vec.at(position - step) == 0) {
            q.push(position - step);
            vec.at(position - step) = vec.at(position) + 1;
        }
        //進むとき
        if (position + step <= n && vec.at(position + step) == 0) {
            q.push(position + step);
            vec.at(position + step) = vec.at(position) + 1;
        }
    }
    if (vec.at(n) == 0) {
        cout << "-1" << endl;
    } else {
        cout << vec.at(n) << endl;
    }
    return 0;
}
//  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...
//  1 1 2 1 2 2 3 1 2  2  3  2  3  3  4  1...
            
            
            
        