Numtri Problem Solving report--icedream61 Blog Park (reproduced please specify the source)
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
There is a pyramid of numbers, shaped as follows
7
3 8
8 1 0
2 7 4 4
4 5 2) 6 5
To start from the top, each time only left or right down, the number and the maximum value.
"Data Range"
1<=r<=1000
0<= each number <=100
"Input Sample"
5
7
3 8
8 1 0
2 7 4 4
4 5 2) 6 5
"Output Example"
30
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
Dp.
Set NUM[I][J] is the number of I row J columns, D[i][j] is the current optimal solution from 1 rows and 1 columns to the I row J column.
D[1][1]=NUM[1][1];
d[i][j]=0; I>1 or J>1
From 1 rows and 1 columns to the maximum value of a column in the R row, it can be directly recursive, the state transition equation is as follows:
When i==1,j>=2, D[i][j]=d[i-1][j]+num[i][j];
When i>=2,j>=2, D[i][j]=max{d[i-1][j]+num[i][j],d[i-1][j-1]+num[i][j]};
But the problem, recursion has boundary conditions, more trouble, recursion more convenient, the state transfer equation is as follows:
When I>=2,2<=j<r
D[I+1][J]=MAX{D[I+1][J],D[I][J]+NUM[I+1][J]};
D[i+1][j+1]=max{d[i+1][j+1],d[i][j]+num[i+1][j+1]};
Finally, just sweep over d[r][1~r] to get the maximum output.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
Once again AC.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 Prob:numtri4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One intR; A intnum[1001][1001],d[1001][1001]; - - voidChangeint&r,intx) {if(X>r) r=x;} the - intMain () - { -Ifstreaminch("numtri.in"); +Ofstream out("Numtri.out"); - + inch>>R; A for(intI=1; i<=r;++i) at for(intj=1; j<=i;++j)inch>>Num[i][j]; - -d[1][1]=num[1][1]; - for(intI=1; i<r;++i) - for(intj=1; j<=i;++j) - { inChange (d[i+1][j],d[i][j]+num[i+1][j]); -Change (d[i+1][j+1],d[i][j]+num[i+1][j+1]); to } + - intmaxsum=0; the for(intj=1; j<=r;++j) Change (Maxsum,d[r][j]); * out<<maxSum<<Endl; $ Panax Notoginseng inch. Close (); - out. Close (); the return 0; +}
Usaco Section1.5 Number triangles problem solving report