Digital triangle series, digital triangle
P1044Digital triangle time: 1000 ms/Space: 131072KiB/Java class name: Main Background: The first bronze medal in USACO November The description shows a digital triangle. Compile a program to calculate a path from top to bottom
Path to maximize the total number of the path.
Each step can go down along the left slash or the right slash;
1 <Number of triangle rows <25;
The number in the triangle is an integer <1000;
The first row N in the input format, indicating N rows.
The N rows below indicate the maximum answer to the sum of the numbers passing by the path right output format path for each triangle Test Example 1 Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Output
30
Remarks search: 80 points; Memory search: AC Memory-based search, which can be accessed from the back. There are two different ways to go normally at each point, so you only need to push back when you go from top to bottom. Therefore, for a [I] [j], you only need to add the maximum values in a [I + 1] [j] And a [I + 1] [j + 1. The maximum value obtained after a continuous loop is a [1] [1].
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int a[30][30],n; 7 int main() 8 { 9 cin>>n;10 for(int i=1;i<=n;i++)for(int j=1;j<=i;j++)scanf("%d",&a[i][j]);11 for(int i=n-1;i>=0;i--)12 {13 for(int j=1;j<=i;j++)14 { 15 a[i][j]+=max(a[i+1][j+1],a[i+1][j]); 16 }17 }18 cout<<a[1][1]<<endl;19 return 0;20 }
P1076Digital triangle 2 time: 1000 ms/Space: 131072KiB/Java class name: Main description digital triangle
The maximum number of mod 100 is required. The input format is 1st rows n, indicating n rows <= 25
2nd to n + 1 actions each weight Maximum output format mod 100 Test Example 1 Input
2
1
99 98
Output
99
The tyvj data is very strange. If you want to cheat the score, output 99 and you will find yourself scored 90 points.