結果

問題 No.34 砂漠の行商人
コンテスト
ユーザー pessimist
提出日時 2026-01-19 07:00:31
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,301 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,865 ms
コンパイル使用メモリ 356,712 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2026-01-19 07:00:36
合計ジャッジ時間 5,276 ms
ジャッジサーバーID
(参考情報)
judge6 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
int L[100][100];
int mxvi[100][100];
const int dirs[]={0,1,0,-1};
void solve(){
    int N,V;cin>>N>>V;
    int Sx,Sy;cin>>Sy>>Sx;--Sx,--Sy;
    int Gx,Gy;cin>>Gy>>Gx;--Gx,--Gy;
    for(int i=0;i<N;++i)
        for(int j=0;j<N;++j)cin>>L[i][j];
    
    int shortest_dist=abs(Sx-Gx)+abs(Gy-Sy);
    if(V>=shortest_dist*9){
        cout<<shortest_dist;
        return;
    }
    
    mxvi[Sx][Sy]=V;
    queue<tuple<int,int,int,int>> que;
    que.emplace(0,Sx,Sy,V);
    while(!que.empty()){
        auto [d,x,y,v]=que.front();
        que.pop();
        for(int k=0;k<4;++k){
            const int nx=x+dirs[k];
            const int ny=y+dirs[k^1];
            if(nx<0||ny<0||nx>=N||ny>=N)continue;
            const int nv=v-L[nx][ny];
            if(mxvi[nx][ny]>=nv)continue;
            const int nd=d+1;
            if(nx==Gx&&ny==Gy){
                cout<<nd;
                return;
            }
            que.emplace(nd,nx,ny,nv);
            mxvi[nx][ny]=nv;
        }
    }
    cout<<-1;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    int T=1; // cin>>T;
    while(T--)solve();
    return 0;
}
0