結果

問題 No.34 砂漠の行商人
ユーザー face4face4
提出日時 2018-11-15 20:54:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 1,125 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 75,024 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 01:22:29
合計ジャッジ時間 2,307 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<queue>
using namespace std;
#define inRange(x,a,b) (a <= x && x < b)
int dx[8] = {0,0,1,-1,1,1,-1,-1};
int dy[8] = {1,-1,0,0,1,-1,1,-1};

struct data{
    int i, j, step, cost;
};

int main(){
    int n, v, si, sj, gi, gj;
    cin >> n >> v >> sj >> si >> gj >> gi;
    si--, sj--, gi--, gj--;

    int l[n][n], d[n][n];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> l[i][j];
            d[i][j] = 1<<21;
        }
    }

    queue<data> q;

    q.push(data({si, sj, 0, 0}));

    while(!q.empty()){
        data da = q.front();  q.pop();
        if(da.i == gi && da.j == gj){
            cout << da.step << endl;
            return 0;
        }

        if(da.cost >= d[da.i][da.j])    continue;

        d[da.i][da.j] = da.cost;

        for(int k = 0; k < 4; k++){
            int ni = da.i + dx[k], nj = da.j + dy[k];
            if(!inRange(ni, 0, n) || !inRange(nj, 0, n) || da.cost + l[ni][nj] >= min(d[ni][nj], v)) continue;
            q.push(data({ni, nj, da.step+1, da.cost+l[ni][nj]}));
        }
    }

    cout << -1 << endl;

    return 0;
}
0