結果

問題 No.1948 足し算するだけのパズルゲーム(1)
ユーザー erbowlerbowl
提出日時 2022-08-07 21:31:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 3,145 bytes
コンパイル時間 3,122 ms
コンパイル使用メモリ 208,380 KB
実行使用メモリ 9,580 KB
最終ジャッジ日時 2023-10-17 16:16:28
合計ジャッジ時間 6,274 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 58 ms
9,580 KB
testcase_08 AC 106 ms
9,580 KB
testcase_09 AC 113 ms
9,580 KB
testcase_10 AC 109 ms
9,580 KB
testcase_11 AC 107 ms
9,544 KB
testcase_12 AC 80 ms
9,580 KB
testcase_13 AC 77 ms
9,580 KB
testcase_14 AC 78 ms
9,580 KB
testcase_15 AC 80 ms
9,580 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 81 ms
9,580 KB
testcase_21 AC 75 ms
9,532 KB
testcase_22 AC 65 ms
9,580 KB
testcase_23 AC 61 ms
9,580 KB
testcase_24 AC 65 ms
9,580 KB
testcase_25 AC 98 ms
9,348 KB
testcase_26 AC 104 ms
9,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
#define int long long

typedef tuple<ll,ll,ll> tll;

signed main(){
    ll h,w,y,x;
    std::cin >> h>>w;
    vector<vector<ll>> a(h,vector<ll>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            std::cin >> a[i][j];
        }
    }
    vector<vector<ll>> b1(h,vector<ll>(w,-1));
    vector<vector<ll>> b2(h,vector<ll>(w,-1));
    b1[0][0]=a[0][0];
    for (int ii = 0; ii < w; ii++) {
        ll x,y;
        x = ii;
        y = 0;
        while(0<=x&&y<h){
            if(b1[y][x]!=-1){
                if(y+1<h){
                    if(a[y+1][x]<b1[y][x]){
                        b1[y+1][x] = max(b1[y+1][x],b1[y][x]+a[y+1][x]);
                    }else{
                        b2[y+1][x] = max(b2[y+1][x],b1[y][x]);
                    }
                }
                if(x+1<w){
                    if(a[y][x+1]<b1[y][x]){
                        b1[y][x+1] = max(b1[y][x+1],b1[y][x]+a[y][x+1]);
                    }else{
                        b2[y][x+1] = max(b2[y][x+1],b1[y][x]);
                    }
                }
            }
            if(b2[y][x]!=-1){
                if(y+1<h){
                    if(a[y+1][x]<b2[y][x]){
                        b2[y+1][x] = max(b2[y+1][x],b2[y][x]+a[y+1][x]);
                    }
                }
                if(x+1<w){
                    if(a[y][x+1]<b2[y][x]){
                        b2[y][x+1] = max(b2[y][x+1],b2[y][x]+a[y][x+1]);
                    }
                }
            }
            // std::cout << x<<" "<<y<<" "<<b1[y][x]<<" "<<b2[y][x] << std::endl;
            x--;
            y++;
        }
    }
    for (int ii = 1; ii < h; ii++) {
        ll x,y;
        x = w-1;
        y = ii;
        while(0<=x&&y<h){
            if(b1[y][x]!=-1){
                if(y+1<h){
                    if(a[y+1][x]<b1[y][x]){
                        b1[y+1][x] = max(b1[y+1][x],b1[y][x]+a[y+1][x]);
                    }else{
                        b2[y+1][x] = max(b2[y+1][x],b1[y][x]);
                    }
                }
                if(x+1<w){
                    if(a[y][x+1]<b1[y][x]){
                        b1[y][x+1] = max(b1[y][x+1],b1[y][x]+a[y][x+1]);
                    }else{
                        b2[y][x+1] = max(b2[y][x+1],b1[y][x]);
                    }
                }
            }
            if(b2[y][x]!=-1){
                if(y+1<h){
                    if(a[y+1][x]<b2[y][x]){
                        b2[y+1][x] = max(b2[y+1][x],b2[y][x]+a[y+1][x]);
                    }
                }
                if(x+1<w){
                    if(a[y][x+1]<b2[y][x]){
                        b2[y][x+1] = max(b2[y][x+1],b2[y][x]+a[y][x+1]);
                    }
                }
            }
            // std::cout << x<<" "<<y<<" "<<b1[y][x]<<" "<<b2[y][x] << std::endl;
            x--;
            y++;
        }
    }
    if((b1[h-1][w-1]!=-1)||(b2[h-1][w-1]!=-1&&b2[h-1][w-1]>a[h-1][w-1])){
        std::cout << "Yes" << std::endl;
    }else{
        std::cout << "No" << std::endl;
    }
}
0