結果

問題 No.3 ビットすごろく
ユーザー _yoshih_
提出日時 2018-09-30 23:35:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 943 bytes
コンパイル時間 798 ms
コンパイル使用メモリ 76,960 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-12 09:20:24
合計ジャッジ時間 1,788 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>

using namespace std;

static int count_bits(int i)
{
    auto b = 0;
    while (i > 0) {
        if (i & 0x01) ++b;
        i >>= 1;
    }

    return b;
}

static int move(const vector<int>& bits, vector<bool>& done, int n, int pos, int count)
{
    if (n == pos) return count;
    if (count > 1 && pos <= 1) return -1;

    if (done[pos]) return -1;
    done[pos] = true;

    if (pos + bits[pos] > n) return move(bits, done, n, pos - bits[pos], count + 1);
    return move(bits, done, n, pos + bits[pos], count + 1);
}

static void run()
{
    int n;
    cin >> n;

    vector<int> bits;
    bits.resize(n + 1);

    for (auto i = 1; i <= n; ++i) {
        bits[i] = count_bits(i);
    }

    vector<bool> done;
    done.resize(bits.size());
    const auto count = move(bits, done, n, 1, 1);
    cout << count << endl;
}

int main(int argc, char **argv)
{
    run();
    return 0;
}
0