Ball Stacking (DP+很有意思) #by Plato


题意:一个数字三角形,取球的规则是取A球的前提是叠在A上的球全部被取了,求最大的得分。

思路:

(我自己没想出来,看了别人代码,但是没看太懂,后来问了下CWK ,然后就懂了)

Ball Stacking (DP+很有意思) #by Plato_第1张图片


#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#define OP(s) cout<<#s<<"="<<s<<" ";
#define PP(s) cout<<#s<<"="<<s<<endl;
using namespace std;

int main()
{
    freopen("test.txt","r",stdin);
    int N;
    while (scanf("%d",&N),N)
    {
        static int a[1009][1009];
        memset(a,0,sizeof(a));
        for (int i =1 ;i <= N;i++)
            for (int j = 1;j <= i;j++)
                scanf("%d",&a[i][j]);

        static int sum[1009][1009];
        memset(sum,0,sizeof(sum));
        for (int i = 1;i <= N;i++)
            for (int j = i;j <= N;j++)
                sum[i][j] = sum[i][j-1] + a[j][i];//sum[i][j] = sum[i][j-1] + a[i][j];

        static int f[1009][1009];
        int ans = 1<<31;
        memset(f,0,sizeof(f));
        for (int i = 1;i <= N;i++)
        {
            int k = N;
            for (int j = N;j >= i;j--)
            {
                if (f[i-1][k] >= f[i-1][j-1])
                    f[i][j] = f[i-1][k];
                else
                {
                    f[i][j] = f[i-1][j-1];
                    k = j-1;
                }

                f[i][j] += sum[i][j];
                if (f[i][j] > ans) ans = f[i][j];
            }
        }

        printf("%d\n",ans > 0?ans:0);
    }

    return 0;
}


你可能感兴趣的:(Ball Stacking (DP+很有意思) #by Plato)