結果

問題 No.1948 足し算するだけのパズルゲーム(1)
ユーザー rogi52rogi52
提出日時 2022-05-20 23:00:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,813 bytes
コンパイル時間 3,465 ms
コンパイル使用メモリ 212,428 KB
実行使用メモリ 588,912 KB
最終ジャッジ日時 2023-10-20 13:52:48
合計ジャッジ時間 7,629 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 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 MLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int H,W; cin >> H >> W;
    vector<vector<ll>> A(H, vector<ll>(W));
    rep(i,H)rep(j,W) cin >> A[i][j];

    auto h = [&](int i, int j){ return i * W + j; };
    auto h_inv = [&](int v){ return pair<int,int>{v / W, v % W}; };
    
    vector<ll> dp(H * W * 2, -1e18);
    auto p = [&](int x, int b){ return x * 2 + b; };
    queue<int> q;
    q.push(h(0, 0));
    dp[p(h(0, 0), 0)] = dp[p(h(0, 0), 1)] = A[0][0];
    int g = h(H - 1, W - 1);
    while(!q.empty()) {
        int from = q.front(); q.pop();
        auto [ci, cj] = h_inv(from);

        if(ci + 1 < H) {
            int i = ci + 1, j = cj;
            int to = h(i, j);
            int E = A[i][j];
            if(dp[p(from, 0)] > E) dp[p(to, 0)] = max(dp[p(to, 0)], dp[p(from, 0)] + E);
            else {
                if(to != g)
                    dp[p(to, 1)] = max(dp[p(to, 1)], dp[p(from, 0)]);
            }

            if(dp[p(from, 1)] > E) {
                dp[p(to, 1)] = max(dp[p(to, 1)], dp[p(from, 1)] + E);
            }
            q.push(to);
        }

        if(cj + 1 < W) {
            int i = ci, j = cj + 1;
            int to = h(i, j);
            int E = A[i][j];
            if(dp[p(from, 0)] > E) dp[p(to, 0)] = max(dp[p(to, 0)], dp[p(from, 0)] + E);
            else {
                if(to != g)
                    dp[p(to, 1)] = max(dp[p(to, 1)], dp[p(from, 0)]);
            }

            if(dp[p(from, 1)] > E) {
                dp[p(to, 1)] = max(dp[p(to, 1)], dp[p(from, 1)] + E);
            }
            q.push(to);
        }
    }
    
    cout << (dp[p(g, 0)] == -1e18 && dp[p(g, 1)] == -1e18 ? "No" : "Yes") << endl;
}
0