結果

問題 No.34 砂漠の行商人
ユーザー momoyuu
提出日時 2024-07-28 01:28:10
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,377 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 105,696 KB
実行使用メモリ 596,044 KB
最終ジャッジ日時 2024-07-28 01:28:22
合計ジャッジ時間 10,696 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
using ll = long long;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n,v;
    cin>>n>>v;
    vector<vector<vector<int>>> dp(n,vector<vector<int>>(n,vector<int>(v+1,1e9)));
    int si,sj,gi,gj;
    cin>>si>>sj>>gi>>gj;
    si--;sj--;gi--;gj--;
    swap(si,sj);
    swap(gi,gj);
    dp[si][sj][v] = 0;
    vector<pair<pair<int,int>,int>> que;
    int dx[] = {1,-1,0,0};
    int dy[] = {0,0,1,-1};  
    
    vector<vector<int>> a(n,vector<int>(n));
    for(int i = 0;i<n;i++) for(int j = 0;j<n;j++) cin>>a[i][j];

    que.push_back(make_pair(make_pair(si,sj),v));
    for(int i = 0;i<que.size();i++){
        auto now = que[i];
        int ni = que[i].first.first;
        int nj = que[i].first.second;
        if(ni==gi&&gj==nj){
            cout<<dp[ni][nj][que[i].second]<<endl;
            return 0;
        }
        int kk = que[i].second;
        for(int k = 0;k<4;k++){
            int nni = ni + dx[k];
            int nnj = nj + dy[k];
            if(nni<0||nnj<0||nni>=n||nnj>=n) continue;
            int nxt = kk - a[nni][nnj];
            if(nxt<=0) continue;
            if(dp[nni][nnj][nxt]<=dp[ni][nj][kk]+1) continue;
            dp[nni][nnj][nxt] = dp[ni][nj][kk] + 1;
            que.push_back({{nni,nnj},nxt});
        }
    }
    cout<<-1<<endl;
}

0