結果

問題 No.34 砂漠の行商人
コンテスト
ユーザー hogeover30
提出日時 2015-03-02 04:55:55
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 49 ms / 5,000 ms
コード長 1,014 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,508 ms
コンパイル使用メモリ 180,800 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-08 16:01:49
合計ジャッジ時間 2,650 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int dx[]={-1, 0, 1, 0}, dy[]={0, -1, 0, 1};
int main()
{
    int n, v, sx, sy, gx, gy;
    while (cin>>n>>v>>sx>>sy>>gx>>gy) {
        vector<vector<int>> L(n, vector<int>(n));
        for(auto& l: L) for(int& w: l) cin>>w;

        queue<tuple<int, int, int, int>> q;
        vector<vector<int>> a(n, vector<int>(n, v));
        --sx, --sy, --gx, --gy;
        q.emplace(0, 0, sy, sx);
        a[sy][sx]=L[sy][sx];
        int res=-1;
        while (q.size()) {
            int t, p, y, x;
            tie(t, p, y, x)=q.front(); q.pop();
            if (x==gx and y==gy) { res=t; break; }
            for(int i=0;i<4;++i) {
                int nx=x+dx[i], ny=y+dy[i];
                if (nx<0 or nx>=n or ny<0 or ny>=n) continue;
                if (p+L[ny][nx]>=a[ny][nx]) continue;
                a[ny][nx]=p+L[ny][nx];
                q.emplace(t+1, p+L[ny][nx], ny, nx);
            }
        }
        cout<<res<<endl;
    }
}

0