POJ3176--Cow Bowling(动态规划)

The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this:
          7

        3   8

      8   1   0

    2   7   4   4

  4   5   2   6   5

Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.

Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int data[][];
int dp[];
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
cin>>data[i][j];
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
for(int j=i;j>=;j--){//这里为何j要逆序开始算呢?等号右边的dp数组是对应的i-1,如果升学计算的话,等号左边被赋值之后,也就意味着i-1数组被修改了,而下次计算正好需要这次修改的原来的数组的值
dp[j]=max(dp[j-],dp[j])+data[i][j];
}
}
int m=dp[];
for(int i=;i<=n;i++){
if(dp[i]>m)
m=dp[i];
}
cout<<m;
return ;
}
 #include<iostream>
#include<string.h>
using namespace std;
int way[MAXN],dp[MAXN];
int N;
void Input()
{
cin>>N;
memset(dp,,sizeof(dp));
for (int i=; i<=N; i++)
{
for (int j=; j<=i; j++)
scanf("%d",&way[j]);
for (int j=i; j>=; j--)
dp[j]=max(dp[j],dp[j-])+way[j];
}
}
void Output()
{
int ret=dp[];
for (int i=; i<=N; i++)
if (ret<dp[i]) ret=dp[i];
cout<<ret<<endl;
}
int main()
{
Input();
Output();
return ;
}
上一篇:es6学习笔记5--promise


下一篇:Codeforces Round #130 (Div. 2)