Cattle Bowling
The direct Chinese
Descriptions
Cows do not use an actual bowling ball when bowling. Each of which takes a number (in the range of 0..99) and arranged in a triangular-shaped standard bowling, as follows:
Given a N (1 <= N <= 350) triangular line, to determine the maximum possible sum that can be achieved.
Cows from the tip is then started through the other triangle and "down" to the two diagonally adjacent one cow, until the "bottom" line. Cows score is the sum of the number of cows to visit along the way. The highest score to win the cows that framework.7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Given a N (1 <= N <= 350) triangular line, to determine the maximum possible sum that can be achieved.
input
Line 1: a single integer, N
row 2 ... N + 1: row i + 1 separated by spaces comprising i integer representing the i-th row of the triangle.
row 2 ... N + 1: row i + 1 separated by spaces comprising i integer representing the i-th row of the triangle.
output
Line 1: The maximum achievable sum of traversal rules
Sample input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
Hint
As indicated above, the highest score may be achieved by crossing cows.7
*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5
Topic Link
Triangle
Looking up from the bottom, from both choose a maximum number that go above and adding the last will be able to obtain the maximum value
AC Code
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include <sstream> #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #define Mod 1000000007 #define eps 1e-6 #define ll long long #define INF 0x3f3f3f3f #define MEM(x,y) memset(x,y,sizeof(x)) #define Maxn 350+5 using namespace std; int main() { int n; int dp[Maxn][Maxn];//三角形 cin>>n; for(int i=1; i<=n; i++)//输入 for(int j=1; j<=i; j++) cin>>dp[i][j]; for(int i=n-1; I> = . 1 ; i--) // grew up to find the optimal solution { for ( int J = . 1 ; J <= I; J ++ ) DP [I] [J] = max (DP [I + . 1 ] [ J], DP [I + . 1 ] [+ J . 1 ]) + DP [I] [J]; } COUT << DP [ . 1 ] [ . 1 ] << endl; // output }