結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2018-11-15 20:54:54 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 62 ms / 5,000 ms | 
| コード長 | 1,125 bytes | 
| コンパイル時間 | 677 ms | 
| コンパイル使用メモリ | 74,624 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-12-24 14:04:56 | 
| 合計ジャッジ時間 | 2,045 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
#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;
}
            
            
            
        