結果

問題 No.3 ビットすごろく
ユーザー kuisibakuisiba
提出日時 2016-04-16 16:54:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,399 bytes
コンパイル時間 525 ms
コンパイル使用メモリ 65,436 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 23:49:24
合計ジャッジ時間 1,883 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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...
0