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.
大致题意:给你一个如上所示的数字金字塔,从第一行开始往下加,每行只能选取一个数字,下一个选取的数字只能位于上一个选取数字的对角线位置上,问最大和为多少
问题分析:我们由下往上取,由于上面的一行的宽度总是小于下面的一行,所以我们到着dp,保证最优子结构,当遍历回第一行是,dp[0][0]即使最优解。
in:
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
out:
30
状态转移方程:a[i][j]+=max(a[i+1][j+1],a[i+1][j]);
#include<iostream> using namespace std; int n; int a[350][350]; int total; int main() { cin>>n; for(int i=0;i<n;i++) for(int j=0;j<=i;j++) cin>>a[i][j]; for(int i=n-2;i>=0;i--) for(int j=0;j<=i;j++) a[i][j]+=max(a[i+1][j+1],a[i+1][j]); cout<<a[0][0]<<endl; return 0; }