Description the cows don ' t use actual bowling balls when they go bowling. They 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 one, diagonally adjacent co WS until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the. The cow with the highest score wins that frame.
Given a triangle with n (1 <= n <=) 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
Hint Explanation of the sample:
7
*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5
The highest score is achievable by traversing the cows as shown above.
Ask to find and Max, the previous line can only be added to the left and right adjacent to the next line ~
Do the first DP, tossing a very long time, a mess of thinking a lot of detour ~
There are two ways to list a table: from bottom to top
1. From top to bottom, more cumbersome, to the end of the last line to take the maximum value.
7 7
3 8 10 15
8 1 0 18 16 15
2 7 4 4 25 23 20 19
4 5 2 6 5 29 30 25 26 24
Number of tables and tables
When the table is listed, you can slowly find the law, get the state equation ~
CODE:
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main ()
{
//freopen ("in.in", "R", stdin);
int dp[355][355];
int num[355][355];
int n;
while (~SCANF ("%d", &n))
{for
(int. i=0;i<n;i++)
{for
(int j=0;j<=i;j++)
scanf (" %d ", &num[i][j]);
}
DP[0][0]=NUM[0][0];
for (int i=0;i<n;i++)
{for
(int j=0;j<n;j++)
{
if (j!=0)
Dp[i+1][j]=max (DP[I][J],DP I [J-1]) +NUM[I+1][J];
if (j==0)
dp[i+1][j]=dp[i][j]+num[i+1][j];
}
}
int maxn=dp[n][0];
for (int i=1;i<n;i++)
{
if (Maxn<dp[n][i]) maxn=dp[n][i];
}
printf ("%d\n", MAXN);
}
return 0;
}
2 from the bottom to the top, the calculation to the top is also to get the maximum, but also like the front of the list can be found rule ~
CODE:
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main ()
{
//freopen ("in.in", "R", stdin);
int dp[355][355];
int num[355][355];
int n;
while (~SCANF ("%d", &n))
{for
(int. i=0;i<n;i++)
{for
(int j=0;j<=i;j++)
scanf (" %d ", &num[i][j]);
}
for (int i=0;i<n;i++)
dp[n-1][i]=num[n-1][i];
for (int i=n-2;i>=0;i--)
{for
(int j=0;j<n;j++)
{
Dp[i][j]=max (dp[i+1][j],dp[i+1][j+1]) +NUM[I][J];
}
}
printf ("%d\n", Dp[0][0]);
}
return 0;
}
14-8-5 now again to do this problem has a new point of entry, with each triangle value as the datum point, select the above route, find the maximum value ~