提出 #1624944


ソースコード 拡げる

#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1000000000000001LL;

int H, W;
string S[50];
int64 dp[55][55];
int64 dp2[55][55][55][55];

int64 rec2(int sy, int sx, int gy, int gx)
{
  return (dp2[sy][sx][gy][gx]);
}

int64 rec(int sy, int sx)
{
  if(sy >= H && sx >= W) return (-INF);
  if(~dp[sy][sx]) return (dp[sy][sx]);
  int64 ret = rec2(sy, sx, H - 1, W - 1);
  for(int i = sy; i < H; i++) {
    for(int j = sx; j < W; j++) {
      if((isdigit(S[i][j]))) {
        if(i + 2 < H && S[i + 1][j] == '+') {
          auto x = rec2(sy, sx, i, j);
          auto y = rec(i + 2, j);
          if(x >= 0 && y >= 0) ret = max(ret, x + y);
        }

        if(j + 2 < W && S[i][j + 1] == '+') {
          auto x = rec2(sy, sx, i, j);
          auto y = rec(i, j + 2);
          if(x >= 0 && y >= 0) ret = max(ret, x + y);
        }

        if((i + 1 < H && S[i + 1][j] == '+') &&
           (j + 1 < W && S[i][j + 1] == '+')) {
          if(i + 1 < H && j + 1 < W) {
            auto x = rec2(sy, sx, i, j);
            auto y = rec(i + 1, j + 1);
            if(x >= 0 && y >= 0) ret = max(ret, x + y);
          }
        }
      }
    }
  }
  return (dp[sy][sx] = min(INF, ret));
}

int main()
{
  memset(dp, -1, sizeof(dp));
  fill_n(***dp2, 55 * 55 * 55 * 55, -INF);

  cin >> H >> W;
  for(int i = 0; i < H; i++) cin >> S[i];


  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      if(isdigit(S[i][j])) {
        dp2[i][j][i][j] = S[i][j] - '0';
        for(int m = i; m < H; m++) {
          for(int n = j; n < W; n++) {
            if(isdigit(S[m][n])) {
              if(dp2[i][j][m][n] >= 0) {
                if(m + 2 < H && S[m + 1][n] == '*') {
                  dp2[i][j][m + 2][n] = max(dp2[i][j][m + 2][n], dp2[i][j][m][n] * (int64) (S[m + 2][n] - '0'));
                  dp2[i][j][m + 2][n] = min(INF, dp2[i][j][m + 2][n]);
                }
                if(n + 2 < W && S[m][n + 1] == '*') {
                  dp2[i][j][m][n + 2] = max(dp2[i][j][m][n + 2], dp2[i][j][m][n] * (int64) (S[m][n + 2] - '0'));
                  dp2[i][j][m][n + 2] = min(INF, dp2[i][j][m][n + 2]);
                }
                if((n + 1 < W && S[m][n + 1] == '*') || (m + 1 < H && S[m + 1][n] == '*')) {
                  if(n + 1 < W && m + 1 < H) {
                    dp2[i][j][m + 1][n + 1] = max(dp2[i][j][m + 1][n + 1], dp2[i][j][m][n] * (int64) (S[m + 1][n + 1] - '0'));
                    dp2[i][j][m + 1][n + 1] = min(INF, dp2[i][j][m + 1][n + 1]);
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  auto p = rec(0, 0);
  if(p >= INF)
    cout << -1 << endl;
  else
    cout << p << endl;
}

提出情報

提出日時
問題 E - Route Calculator
ユーザ ei1333333333
言語 C++14 (GCC 5.4.1)
得点 100
コード長 2819 Byte
結果 AC
実行時間 32 ms
メモリ 71808 KiB

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 39
セット名 テストケース
All 00_sample_00, 00_sample_01, 00_sample_02, 00_sample_03, 10_small_0, 10_small_1, 10_small_2, 10_small_3, 10_small_4, 10_small_5, 10_small_6, 10_small_7, 10_small_8, 10_small_9, 20_random_0, 20_random_1, 20_random_2, 20_random_3, 20_random_4, 20_random_5, 20_random_6, 20_random_7, 20_random_8, 20_random_9, 30_unbalance00, 30_unbalance01, 30_unbalance02, 30_unbalance03, 30_unbalance04, 30_unbalance05, 30_unbalance06, 30_unbalance07, 30_unbalance08, 30_unbalance09, 30_unbalance10, 90_custom0, 90_custom1, 90_custom2, 90_custom3
ケース名 結果 実行時間 メモリ
00_sample_00 AC 20 ms 71808 KiB
00_sample_01 AC 20 ms 71808 KiB
00_sample_02 AC 20 ms 71808 KiB
00_sample_03 AC 20 ms 71808 KiB
10_small_0 AC 20 ms 71808 KiB
10_small_1 AC 20 ms 71808 KiB
10_small_2 AC 20 ms 71808 KiB
10_small_3 AC 20 ms 71808 KiB
10_small_4 AC 20 ms 71808 KiB
10_small_5 AC 20 ms 71808 KiB
10_small_6 AC 20 ms 71808 KiB
10_small_7 AC 20 ms 71808 KiB
10_small_8 AC 20 ms 71808 KiB
10_small_9 AC 20 ms 71808 KiB
20_random_0 AC 20 ms 71808 KiB
20_random_1 AC 20 ms 71808 KiB
20_random_2 AC 20 ms 71808 KiB
20_random_3 AC 21 ms 71808 KiB
20_random_4 AC 21 ms 71808 KiB
20_random_5 AC 20 ms 71808 KiB
20_random_6 AC 20 ms 71808 KiB
20_random_7 AC 20 ms 71808 KiB
20_random_8 AC 20 ms 71808 KiB
20_random_9 AC 20 ms 71808 KiB
30_unbalance00 AC 32 ms 71808 KiB
30_unbalance01 AC 32 ms 71808 KiB
30_unbalance02 AC 31 ms 71808 KiB
30_unbalance03 AC 30 ms 71808 KiB
30_unbalance04 AC 29 ms 71808 KiB
30_unbalance05 AC 29 ms 71808 KiB
30_unbalance06 AC 29 ms 71808 KiB
30_unbalance07 AC 29 ms 71808 KiB
30_unbalance08 AC 28 ms 71808 KiB
30_unbalance09 AC 27 ms 71808 KiB
30_unbalance10 AC 26 ms 71808 KiB
90_custom0 AC 29 ms 71808 KiB
90_custom1 AC 26 ms 71808 KiB
90_custom2 AC 20 ms 71808 KiB
90_custom3 AC 20 ms 71808 KiB