AcWing - 275 - 传纸条 - dp

https://www.acwing.com/problem/content/277/
按对角线为阶段,记录x上的状态推断y进行dp,注意两个取值都是n!

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

ll a[55][55];
ll dp[105][55][55];

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            scanf("%lld", &a[i][j]);
        }
    }
    for(int t = 2; t <= (n + m); ++t) {
        for(int i = 1; i <= min(n, t - 1); ++i) {
            for(int j = 1; j <= min(n, t - 1); ++j) {
                dp[t][i][j] = max(max(dp[t - 1][i - 1][j - 1], dp[t - 1][i - 1][j]), max(dp[t - 1][i][j - 1], dp[t - 1][i][j]))
                              + a[i][t - i] + ((i == j) ? (0) : (a[j][t - j]));
                //printf("dp[%d][%d][%d]=%lld\n",t,i,j,dp[t][i][j]);
            }
        }
    }
    printf("%lld\n", dp[n + m][n][n]);
}

AcWing - 275 - 传纸条 - dp

上一篇:[C#教程01]C# 简介


下一篇:C#月份和日期转大写和C#集合分组