結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 4 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 <ctype.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

#define INF 1000000000

int calc_fastest(int pos, int squares[], int goal_idx, vector<int> *footpirnt)
{
    if (pos == goal_idx)
    {
        return 0;
    }

    if (any_of(footpirnt->begin(), footpirnt->end(), [pos](int x) { return x == pos; }))
    {
        return INF;
    }

    footpirnt->push_back(pos);

    int distance = squares[pos];

    int steps_when_forward = INF;
    int steps_when_backword = INF;

    if (pos + distance <= goal_idx)
    {
        steps_when_forward = 1 + calc_fastest(pos + distance, squares, goal_idx, footpirnt);
    }

    if (pos - distance >= 0)
    {
        steps_when_backword = 1 + calc_fastest(pos - distance, squares, goal_idx, footpirnt);
    }

    return min(steps_when_forward, steps_when_backword);
}

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

    int index = 0;
    int squares[n];

    for (int max = 1; 1; max++)
    {
        for (int i = 1; i <= max; i++)
        {
            if (index + 1 > n)
                goto BREAK_LABEL;

            squares[index] = i;
            index++;
        }
    }

BREAK_LABEL:

    vector<int> footprint{};
    int fastest = 1 + calc_fastest(0, squares, n - 1, &footprint);

    if (fastest >= INF)
    {
        printf("-1");
    }
    else
    {
        printf("%d", fastest);
    }
}
0