結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー MazesobaMazesoba
提出日時 2022-05-21 00:05:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 3,000 ms
コード長 1,772 bytes
コンパイル時間 4,680 ms
コンパイル使用メモリ 270,480 KB
実行使用メモリ 6,104 KB
最終ジャッジ日時 2023-10-20 15:00:19
合計ジャッジ時間 7,967 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 77 ms
4,776 KB
testcase_08 AC 111 ms
4,760 KB
testcase_09 AC 130 ms
6,104 KB
testcase_10 AC 131 ms
6,104 KB
testcase_11 AC 129 ms
6,092 KB
testcase_12 AC 97 ms
5,384 KB
testcase_13 AC 59 ms
6,104 KB
testcase_14 AC 90 ms
6,104 KB
testcase_15 AC 89 ms
6,104 KB
testcase_16 AC 48 ms
4,760 KB
testcase_17 AC 89 ms
6,104 KB
testcase_18 AC 47 ms
4,760 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 76 ms
6,060 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 49 ms
5,384 KB
testcase_25 AC 97 ms
4,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;

//#define DISABLE_PRINT

#if defined(ENABLE_PRINT) && !defined(DISABLE_PRINT)

#define P(...) fprintf(stderr, __VA_ARGS__)
#define P2(fmt) fprintf(stderr, fmt)
#define LP fprintf(stderr, "L: %d\n", __LINE__)

#else

#define P(...) ((void)0)
#define P2(fmt) ((void)0)
#define LP ((void)0)

#endif

#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define ALL(x) x.begin(),x.end()

using ll = long long;
using ull = unsigned long long;

pair<int, int> diffs[] = {
    {0, 1}, {0, -1}, {1, 0}, {-1, 0}
};

int main(int, const char**)
{
    int H, W, Y, X; cin >> H >> W >> Y >> X;
    Y--; X--;
    struct V {
        int y;
        int x;
        int v;
    };
    vector<vector<bool>> done(H, vector<bool>(W));
    vector<vector<int>> A(H, vector<int>(W));
    rep(i, H) rep(j, W) cin >> A[i][j];
    ll p = 0;
    p += A[Y][X];

    auto comp = [](const V& a, const V& b) {
        return a.v > b.v;
    };

    priority_queue<V, vector<V>, decltype(comp)> q{comp};

    auto push = [&](int y, int x) {
        P("pos(%d, %d)\n", y, x);
        assert(done[y][x]);
        for(auto diff : diffs) {
            auto ny = y + diff.first;
            auto nx = x + diff.second;
            if(ny < 0 || nx < 0 || ny >= H || nx >= W) continue;
            if(done[ny][nx]) continue;
            q.push({ny, nx, A[ny][nx]});
            done[ny][nx] = true;
        }
    };

    done[Y][X] = true;
    push(Y, X);

    int count = 0;
    while(!q.empty()) {
        auto t = q.top(); q.pop();
        if(p <= t.v) break;

        p += t.v;
        push(t.y, t.x);
        count++;
    }

    if(count == H * W - 1) {
        cout << "Yes" << endl;
    }
    else cout << "No" << endl;

    return 0;
}
0