結果

問題 No.34 砂漠の行商人
ユーザー koyoprokoyopro
提出日時 2015-09-29 20:20:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,416 bytes
コンパイル時間 1,403 ms
コンパイル使用メモリ 150,316 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-10 19:16:46
合計ジャッジ時間 2,734 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

#define REP(i, n) for(int i=0; i<(n); i++)

int N,V;
typedef pair<int, int> pos;
int pos::*x = &pos::first;
    int pos::*y = &pos::second;
pos s, g;
int L[200][200];
int HP[200][200];
int C[200][200];
int INF = 1e9;
signed main()
{
    cin >> N >> V>>s.*x>>s.*y>>g.*x>>g.*y;
    s.*x -= 1;
    s.*y -= 1;
    g.*x -= 1;
    g.*y -= 1;
    REP(y,N)REP(x,N) cin >> L[x][y];
    REP(y,N)REP(x,N) {
        HP[x][y] = INF;
        C[x][y] = INF;
    }

    HP[s.*x][s.*y] = 0; // スタート地点は消費HP0で行ける
    C[s.*x][s.*y] = 0; // スタート地点は0移動で行ける
    queue<pos> que;
    que.push(s);
    int dxy[] = {0, 1, 0, -1, 0};
    while(que.size()) {
        pos p = que.front();
        que.pop();
        REP(i,4) {
            int nx = p.*x + dxy[i];
            int ny = p.*y + dxy[i+1];
            if (nx<0||N<=nx||ny<0||N<=ny) continue;
            if (L[nx][ny] + HP[p.*x][p.*y] >= V) continue; // 死ぬので行けない
            if (nx == g.*x && ny == g.*y) {
                cout << C[p.*x][p.*y] + 1 << endl;
                return 0;
            }
            if (HP[nx][ny] > L[nx][ny] + HP[p.*x][p.*y]) {
                HP[nx][ny] = L[nx][ny] + HP[p.*x][p.*y];
                C[nx][ny] = C[p.*x][p.*y] + 1;
                que.push(pos(nx, ny));
            }
        }
    }
    cout << -1 << endl;
    return 0;
}
0