結果

問題 No.34 砂漠の行商人
ユーザー KudeKude
提出日時 2020-09-05 23:04:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,460 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 174,936 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-19 13:12:35
合計ジャッジ時間 2,382 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i>=0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int mxvit[100][100];
int l[100][100];
const int INF = 10000;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
    int n, v, sx, sy, gx, gy;
    scanf("%d%d%d%d%d%d", &n, &v, &sy, &sx, &gy, &gx);
    --sx; --sy; --gx; --gy;
    rep(i, n) rep(j, n) scanf("%d", &l[i][j]);
    int shortest_dist = abs(sx - gx) + abs(sy - gy);
    if (v > shortest_dist * 9) {
        printf("%d\n", shortest_dist);
        return 0;
    }
    queue<tuple<int,int,int,int>> q;
    mxvit[sx][sy] = v;
    q.emplace(0, sx, sy, v);
    while(!q.empty()) {
        int d, x, y, vit;
        tie(d, x, y, vit) = q.front(); q.pop();
        rrep(k, 4) {
            int nx = x + dx[k], ny = y + dy[k];
            if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
            int nvit = vit - l[nx][ny];
            int nd = d + 1;
            if (nvit <= mxvit[nx][ny]) continue;
            if (nx == gx && ny == gy) {
                printf("%d\n", nd);
                return 0;
            }
            mxvit[nx][ny] = nvit;
            q.emplace(nd, nx, ny, nvit);
        }
    }
    printf("-1\n");
}
0