結果

問題 No.34 砂漠の行商人
ユーザー MisterMister
提出日時 2020-08-22 05:53:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,283 bytes
コンパイル時間 909 ms
コンパイル使用メモリ 93,592 KB
実行使用メモリ 274,848 KB
最終ジャッジ日時 2024-04-23 06:56:09
合計ジャッジ時間 7,664 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 10 ms
5,376 KB
testcase_05 AC 9 ms
5,632 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 15 ms
6,784 KB
testcase_08 AC 22 ms
8,576 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <tuple>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

const std::vector<std::pair<int, int>>
    dxys{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void solve() {
    int n, m, sx, sy, gx, gy;
    std::cin >> n >> m >> sy >> sx >> gy >> gx;
    --sx, --sy, --gx, --gy;

    auto xss = vec(n, vec(n, 0));
    for (auto& xs : xss) {
        for (auto& x : xs) std::cin >> x;
    }

    auto dp = vec(m, vec(n, vec(n, -1)));
    std::queue<std::tuple<int, int, int>> que;
    for (int d = 0; d < m; ++d) {
        dp[d][sx][sy] = 0;
        que.emplace(d, sx, sy);
    }

    while (!que.empty()) {
        auto [d, x, y] = que.front();
        que.pop();

        for (auto [dx, dy] : dxys) {
            int nx = x + dx,
                ny = y + dy;
            if (nx < 0 || n <= nx ||
                ny < 0 || n <= ny) continue;

            int nd = d - xss[nx][ny];
            if (nd < 0 || dp[nd][nx][ny] != -1) continue;
            dp[nd][nx][ny] = dp[d][x][y] + 1;
            que.emplace(nd, nx, ny);
        }
    }

    std::cout << dp[0][gx][gy] << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0