結果

問題 No.34 砂漠の行商人
ユーザー kyunakyuna
提出日時 2019-07-30 18:46:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,547 bytes
コンパイル時間 1,013 ms
コンパイル使用メモリ 78,524 KB
実行使用メモリ 402,944 KB
最終ジャッジ日時 2023-09-18 14:14:46
合計ジャッジ時間 15,619 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
394,328 KB
testcase_01 AC 277 ms
394,312 KB
testcase_02 AC 218 ms
394,320 KB
testcase_03 AC 219 ms
394,380 KB
testcase_04 AC 229 ms
394,160 KB
testcase_05 AC 227 ms
394,396 KB
testcase_06 AC 219 ms
394,064 KB
testcase_07 AC 233 ms
394,140 KB
testcase_08 AC 236 ms
394,276 KB
testcase_09 AC 4,367 ms
394,648 KB
testcase_10 TLE -
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 <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);
            }
        }
    }
    int time = INF;
    for (int hp = 1; hp <= v; hp++) chmin(time, dp[gi][gj][hp]);
    cout << (time == INF ? -1 : time) << endl;
    return 0;
}
0