結果

問題 No.34 砂漠の行商人
ユーザー kyunakyuna
提出日時 2019-07-30 19:00:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,211 ms / 5,000 ms
コード長 1,588 bytes
コンパイル時間 1,544 ms
コンパイル使用メモリ 79,392 KB
実行使用メモリ 395,528 KB
最終ジャッジ日時 2023-09-18 14:37:41
合計ジャッジ時間 12,090 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 224 ms
394,028 KB
testcase_01 AC 221 ms
394,032 KB
testcase_02 AC 221 ms
394,044 KB
testcase_03 AC 221 ms
394,180 KB
testcase_04 AC 214 ms
394,244 KB
testcase_05 AC 209 ms
394,096 KB
testcase_06 AC 201 ms
394,112 KB
testcase_07 AC 215 ms
394,100 KB
testcase_08 AC 217 ms
394,356 KB
testcase_09 AC 462 ms
394,924 KB
testcase_10 AC 199 ms
394,208 KB
testcase_11 AC 823 ms
395,120 KB
testcase_12 AC 182 ms
394,124 KB
testcase_13 AC 1,211 ms
395,456 KB
testcase_14 AC 938 ms
395,424 KB
testcase_15 AC 179 ms
394,124 KB
testcase_16 AC 208 ms
394,504 KB
testcase_17 AC 178 ms
394,096 KB
testcase_18 AC 179 ms
394,076 KB
testcase_19 AC 351 ms
395,060 KB
testcase_20 AC 502 ms
395,528 KB
testcase_21 AC 177 ms
394,104 KB
testcase_22 AC 180 ms
394,292 KB
testcase_23 AC 180 ms
394,196 KB
testcase_24 AC 611 ms
395,320 KB
testcase_25 AC 199 ms
394,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAX_N = 100;
const int MAX_V = 10000;
const int INF = (int)1e9 + 7;
using pii = pair<int, int>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int dp[MAX_N][MAX_N][MAX_V + 1];

int main() {
    int n, v, sj, si, gj, gi;
    cin >> n >> v >> sj >> si >> gj >> gi; sj--, si--, gj--, gi--;
    for (int i = 0; i < MAX_N; i++) for (int j = 0; j < MAX_N; j++) {
        fill_n(dp[i][j], MAX_V + 1, INF);
    }
    int dmg[MAX_N][MAX_N] = {};
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> dmg[i][j];
    queue<pair<pii, int>> que;  // <i, j>, hp
    que.emplace(pii(si, sj), v); dp[si][sj][v] = 0;
    int dij[] = {0, 1, 0, -1, 0};
    auto out_field = [&](int i, int j) {
        return i < 0 || i >= n || j < 0 || j >= n;
    };
    while (!que.empty()) {
        auto p = que.front(); que.pop();
        int i = p.first.first, j = p.first.second, hp = p.second;
        for (int k = 0; k < 4; k++) {
            int ni = i + dij[k], nj = j + dij[k + 1];
            if (out_field(ni, nj)) continue;
            int next_hp = hp - dmg[ni][nj];
            if (next_hp <= 0) continue;
            if (chmin(dp[ni][nj][next_hp], dp[i][j][hp] + 1)) {
                que.emplace(pii(ni, nj), next_hp);
                if (ni == gi && nj == gj) {
                    cout << dp[ni][nj][next_hp] << endl;
                    return 0;
                }
            }
        }
    }
    cout << -1 << endl;
    return 0;
}
0