天天看點

累加型DP—— [Usaco2006 Oct]Cow Pie Treasures 餡餅裡的财富

http://www.zybbs.org/JudgeOnline/problem.php?id=1668

類似于數字金字塔的DP

先對數字處理

如:

1 2 3 4

就要處理成

1 2 0 0

0 2 3 0

0 0 3 4

因為是起點是左上角,終點是右下角,友善dp方程,是以指派為‘0’,

累加型DP—— [Usaco2006 Oct]Cow Pie Treasures 餡餅裡的财富
累加型DP—— [Usaco2006 Oct]Cow Pie Treasures 餡餅裡的财富
View Code

#include<stdio.h>
#include<iostream>
using namespace std;
int dp[109][109];

int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
    {
int i,j;

for(i=1;i<=n;i++)
        {
for(j=1;j<=m;j++)
            {
                scanf("%d",&dp[i][j]);
            }
        }

for(i=2;i<=n;i++)
        {
for(j=1;j<=(i-1);j++)
            {
                dp[i][j]=0;
                dp[n+1-i][m+1-j]=0;
            }
        }

for(j=m-1;j>=1;j--)
        {
for(i=n;i>=1;i--)
            {
                    dp[i][j]+=max(max(dp[i][j+1],dp[i-1][j+1]),dp[i+1][j+1]);
            }
        }

        printf("%d\n",dp[1][1]);
    }
}