結果

問題 No.20 砂漠のオアシス
ユーザー Shibuyap
提出日時 2020-10-24 19:18:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 2,155 bytes
コンパイル時間 2,258 ms
コンパイル使用メモリ 199,372 KB
最終ジャッジ日時 2025-01-15 15:21:47
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("YES");}else{puts("NO");}
#define MAX_N 200005

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int main() {
    int n, v, ox, oy;
    cin >> n >> v >> ox >> oy;
    swap(ox,oy);
    int a[n+2][n+2];
    int f[n+2][n+2][2];
    rep(i,n+2){
        rep(j,n+2){
            if(i==0||i==n+1||j==0||j==n+1){
                a[i][j] = 1001001001;
            }else{
                cin >> a[i][j];
            }
            f[i][j][0] = 0;
            f[i][j][1] = 0;
        }
    }

    priority_queue<pair<P,P>> que;
    pair<P,P> p;
    p.first.first = v;
    p.first.second = 0;
    p.second.first = 1;
    p.second.second = 1;
    que.push(p);
    f[1][1][0] = v;

    while(que.size() > 0){
        int v = que.top().first.first;
        int z = que.top().first.second;
        int x = que.top().second.first;
        int y = que.top().second.second;
        que.pop();
        if(f[x][y][z] > v) continue;

        // cout << x << ' ' << y << ' ' << z << ' ' << v << endl;

        rep(i,4){
            int nx = x + dx[i];
            int ny = y + dy[i];
            int nv = v - a[nx][ny];
            if(nv <= f[nx][ny][z]) continue;
            if(nx==ox&&ny==oy){
                nv *= 2;
                if(f[nx][ny][1] < nv){
                    f[nx][ny][1] = nv;
                    p.first.first = nv;
                    p.first.second = 1;
                    p.second.first = nx;
                    p.second.second = ny;
                    que.push(p);
                }
            }else{
                if(f[nx][ny][z] < nv){
                    f[nx][ny][z] = nv;
                    p.first.first = nv;
                    p.first.second = z;
                    p.second.first = nx;
                    p.second.second = ny;
                    que.push(p);
                }
            }
        }
    }

    if(f[n][n][0]>0||f[n][n][1]>0) yn;
    return 0;
}
 
 
0