結果

問題 No.20 砂漠のオアシス
ユーザー xuzijian629xuzijian629
提出日時 2018-11-02 22:21:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 1,873 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 209,456 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-12 22:53:40
合計ジャッジ時間 3,476 ms
ジャッジサーバーID
(参考情報)
judge14 / judge8
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,388 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 9 ms
4,388 KB
testcase_06 AC 8 ms
4,384 KB
testcase_07 AC 27 ms
4,380 KB
testcase_08 AC 10 ms
4,384 KB
testcase_09 AC 19 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,384 KB
testcase_16 AC 4 ms
4,384 KB
testcase_17 AC 4 ms
4,384 KB
testcase_18 AC 4 ms
4,384 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

int main() {
    int n, v, ox, oy;
    cin >> n >> v >> oy >> ox;
    ox--;
    oy--;

    vvi b(n, vi(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> b[i][j];
        }
    }

    int dx[] = {-1, 1, 0, 0};
    int dy[] = {0, 0, -1, 1};
    auto ok = [&](int a, int b) {
        return 0 <= a && a < n && 0 <= b && b < n;
    };

    vvi d(n, vi(n, -1));
    d[0][0] = v;
    using ii = pair<int, int>;
    queue<ii> que;
    que.push(ii(0, 0));
    while (que.size()) {
        ii t = que.front();
        que.pop();
        for (int i = 0; i < 4; i++) {
            int x = t.first + dx[i];
            int y = t.second + dy[i];
            if (ok(x, y)) {
                int vv = d[t.first][t.second] - b[x][y];
                if (vv > 0 && d[x][y] < vv) {
                    d[x][y] = vv;
                    que.push(ii(x, y));
                }
            }
        }
    }

    if (d[n - 1][n - 1] != -1) {
        cout << "YES" << endl;
        return 0;
    }

    if ((ox == -1 && oy == -1) || d[ox][oy] == -1) {
        cout << "NO" << endl;
        return 0;
    }

    v = d[ox][oy] * 2;

    d = vvi(n, vi(n, -1));
    d[ox][oy] = v;
    using ii = pair<int, int>;
    que.push(ii(ox, oy));
    while (que.size()) {
        ii t = que.front();
        que.pop();
        for (int i = 0; i < 4; i++) {
            int x = t.first + dx[i];
            int y = t.second + dy[i];
            if (ok(x, y)) {
                int vv = d[t.first][t.second] - b[x][y];
                if (vv > 0 && d[x][y] < vv) {
                    d[x][y] = vv;
                    que.push(ii(x, y));
                }
            }
        }
    }

    cout << (d[n - 1][n - 1] == -1 ? "NO" : "YES") << endl;
}
0