結果

問題 No.1948 足し算するだけのパズルゲーム(1)
ユーザー ku_senjanku_senjan
提出日時 2023-06-07 09:53:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 1,264 bytes
コンパイル時間 2,003 ms
コンパイル使用メモリ 166,680 KB
実行使用メモリ 9,516 KB
最終ジャッジ日時 2023-08-28 14:21:18
合計ジャッジ時間 5,833 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,416 KB
testcase_01 AC 2 ms
5,428 KB
testcase_02 AC 2 ms
5,648 KB
testcase_03 AC 2 ms
5,428 KB
testcase_04 AC 2 ms
5,420 KB
testcase_05 AC 2 ms
5,372 KB
testcase_06 AC 2 ms
5,468 KB
testcase_07 AC 54 ms
9,408 KB
testcase_08 AC 106 ms
9,332 KB
testcase_09 AC 106 ms
9,412 KB
testcase_10 AC 107 ms
9,376 KB
testcase_11 AC 105 ms
9,320 KB
testcase_12 AC 78 ms
9,320 KB
testcase_13 AC 76 ms
9,460 KB
testcase_14 AC 78 ms
9,516 KB
testcase_15 AC 77 ms
9,348 KB
testcase_16 AC 3 ms
5,428 KB
testcase_17 AC 4 ms
8,332 KB
testcase_18 AC 2 ms
5,452 KB
testcase_19 AC 2 ms
5,444 KB
testcase_20 AC 77 ms
9,344 KB
testcase_21 AC 77 ms
9,372 KB
testcase_22 AC 64 ms
9,412 KB
testcase_23 AC 64 ms
9,348 KB
testcase_24 AC 63 ms
9,428 KB
testcase_25 AC 96 ms
9,276 KB
testcase_26 AC 99 ms
9,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define MAX_H 510
#define MAX_W 510

int H, W;
ll A[MAX_H][MAX_W], dp[MAX_H][MAX_W][2];

template<class T>
bool chmax(T &a, T b){
  if(a<b){
    a=b; return true;
  }
  return false;
}

int main(void){
  cin >> H >> W;
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      cin >> A[i][j];
    }
  }

  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      for(int k=0; k<2; k++){
        dp[i][j][k] = -1;
      }
    }
  }

  dp[0][0][0] = A[0][0];

  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      for(int k=0; k<2; k++){
        if(dp[i][j][k]==-1) continue;
        if(i+1<H){
          if(A[i+1][j]<dp[i][j][k]){
            chmax(dp[i+1][j][k], dp[i][j][k]+A[i+1][j]);
          }else if(k==0 && (i+1<H-1 || j<W-1)){
            chmax(dp[i+1][j][1], dp[i][j][k]);
          }
        }
        if(j+1<W){
          if(A[i][j+1]<dp[i][j][k]){
            chmax(dp[i][j+1][k], dp[i][j][k]+A[i][j+1]);
          }else if(k==0 && (i<H-1 || j+1<W-1)){
            chmax(dp[i][j+1][1], dp[i][j][k]);
          }
        }
      }
    }
  }

  bool ans = true;
  if(dp[H-1][W-1][0]==-1 && dp[H-1][W-1][1]==-1) ans = false;

  cout << (ans?"Yes":"No") << endl;

  return 0;
}
0