結果

問題 No.3 ビットすごろく
ユーザー morinagamorinaga
提出日時 2020-02-16 17:12:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,200 bytes
コンパイル時間 452 ms
コンパイル使用メモリ 55,168 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-16 03:37:02
合計ジャッジ時間 1,538 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <vector>
#include <queue>

#define UKN -1

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);

    vector<int> squares(n, 0);
    for (int i = 0; i < n; i++)
    {
        unsigned int v = i + 1;
        unsigned int c;

        for (c = 0; v; v >>= 1)
        {
            c += v & 1;
        }

        squares[i] = c;
    }

    vector<int> least_steps(n, UKN);
    least_steps[0] = 1;

    queue<int> reachable_sqs;
    reachable_sqs.push(0);

    int goal_idx = n - 1;

    while (!reachable_sqs.empty())
    {
        int cur_idx = reachable_sqs.front();
        reachable_sqs.pop();

        int propulsion = squares[cur_idx];

        if (cur_idx + propulsion <= goal_idx && least_steps[cur_idx + propulsion] == UKN)
        {
            least_steps[cur_idx + propulsion] = least_steps[cur_idx] + 1;
            reachable_sqs.push(cur_idx + propulsion);
        }

        if (cur_idx - propulsion >= 0 && least_steps[cur_idx + propulsion] == UKN)
        {
            least_steps[cur_idx - propulsion] = least_steps[cur_idx] + 1;
            reachable_sqs.push(cur_idx - propulsion);
        }
    }

    printf("%d", least_steps[goal_idx]);
}
0