結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
5,568 KB
testcase_02 AC 5 ms
18,172 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 16 ms
41,204 KB
testcase_05 AC 18 ms
44,640 KB
testcase_06 AC 6 ms
11,396 KB
testcase_07 AC 30 ms
26,212 KB
testcase_08 AC 31 ms
54,668 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 13 ms
15,624 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 166 ms
60,504 KB
testcase_20 AC 268 ms
80,980 KB
testcase_21 AC 22 ms
63,276 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 30 ms
50,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 dist[100][100][2000];
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;
    }
    rrep(i, n)rrep(j, n)rrep(k, v + 1) dist[i][j][k] = INF;
    queue<tuple<int,int,int,int>> q;
    dist[sx][sy][v] = 0;
    q.emplace(0, sx, sy, v);
    while(!q.empty()) {
        int d, x, y, vit;
        tie(d, x, y, vit) = q.front(); q.pop();
        if (d > dist[x][y][vit]) continue;
        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 <= 0) continue;
            if (nx == gx && ny == gy) {
                printf("%d\n", nd);
                return 0;
            }
            if (nd < dist[nx][ny][nvit]) {
                dist[nx][ny][nvit] = nd;
                q.emplace(nd, nx, ny, nvit);
            }
        }
    }
    printf("-1\n");
}
0