AcWing 275. 传纸条(数字三角模型)

 

 

 

  分析:本题可以采用方格取数的代码,证明如下

 

 

 

 

 

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

const int N = 55;
int a[N][N], f[2*N][N][N];
int n, m;


int main() {
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) cin >> a[i][j];
    }
    for(int k = 2; k < m + n; k++) {
        for(int x1 = 1; x1 <= min(k,m); x1++) {
            for(int x2 = 1; x2 <= min(k,m); x2++) {
                int y1 = k + 1 - x1, y2 = k + 1 - x2;
                int t = a[y1][x1];
                int &v = f[k][x1][x2];
                if(x1 != x2) t += a[y2][x2];
                v = max(f[k-1][x1][x2],max(f[k-1][x1-1][x2-1],max(f[k-1][x1-1][x2],f[k-1][x1][x2-1])));
                v += t;
            }
        }
    }
    printf("%d\n",f[n+m-1][m][m]);
    return 0;
}

 

posted @ 2020-09-04 16:30  Sexyomaru  阅读(184)  评论(0编辑  收藏  举报