Description
Consider the number triangle shown below. Write A program this calculates the highest sum of numbers that can is passed on a route this starts at the top and ends s Omewhere on the base. Each of the step can go either diagonally down to the left or diagonally down to the right.
7 3 8 8 1 0 2 7 4 44 5 2 6 5
In the sample above, the route from 7 through 3 to 8 to 7 to 5 produces the highest sum:30.
Input Format
The first line contains R (1 <= R <=), the number of rows. Each subsequent line contains the integers for that particular row of the triangle. All the supplied integers is non-negative and no larger than 100.
Output Format
A single line containing the largest sum using the traversal specified.
Sample Input:
573 88 1 02 7 4 44 5 2 6 5
Sample Output:
30
Using the idea of dynamic programming, you can update a node from the second row, making the value of the node the maximum value that was selected before it. The language narrative is troublesome, in the case of
7
10 15
18 16 15
20 25 20 19
24 30 27) 26 24
So choose the largest number in the last row, but this update method is more troublesome to consider the boundary situation, such as the processing of two endpoints per row is more troublesome
The process of locating a node's precursor node is related to the node location "
Is it more convenient to find the successor node of a node ? Yes, because all nodes have two child nodes.
So the update process changes to the first row from the second-to-last row.
Last row not moving: 4 5 2 6 5
The penultimate row starts to update:
7 12 10 10
20 13 10
23 21
30
So the first node after the update is the answer and avoids the process of finding the maximum value, which is very simple.
This algorithm belongs to the idea of dynamic programming, because it stores the results of a potentially repetitive calculation (or, in essence, no duplicate computations), and each phase is a set of states for each row.
The state of each point s[x] means, on the basis of the original tree, from this point onwards to the final path of the largest and.
State transition equation is s[x] = r[x] + max (S[x.leftson],s[x.rightson])
The next layer of x used to calculate the state also implies that we are going to traverse from the bottom up.
The code is as follows:
#include <iostream>using namespacestd;intr[1000000] = {0};intN;inlineintGetlayfirstid (intLayid) { return(1+layid-1) * (layid-1)/2+1;} InlineintGetleftson (intnode,intLayid) { returnGetlayfirstid (layid+1) + node-Getlayfirstid (Layid);}//from Curid start down select Curlay is the current line cursum is before andintCalc (intCurid,intCurlay,intcursum) { intleft =Getleftson (Curid,curlay); intright = left+1; if(Curlay = = N1) returnR[left] > R[right]? Cursum + R[left]: Cursum +R[right]; intLsum = Calc (left,curlay+1, cursum) +R[curid]; intRsum = Calc (right,curlay+1, cursum) +R[curid]; returnLsum > Rsum?Lsum:rsum;} InlineintGetmax (intAintb) { returnA>b?a:b;}//start updating from N-1 rowInlinevoidUpdatelayer (intLayid) { for(inti =0; i < Layid; ++i) {intnode = Getlayfirstid (Layid) +i; intleft =Getleftson (Node,layid); intright = left+1; R[node]+=Getmax (R[left],r[right]); }}intMainintargcChar Const*argv[]) {cin>>N; for(inti =1; I <= N (n+1)/2; ++i) Cin>>R[i];//start storage from r[1] for ease of calculation for(intK = n1; K >=1; k--) Updatelayer (k); cout<<r[1]<<Endl; return 0;}
View Code
PS1: This problem with greedy method can get 60 points, greedy strategy is decided to choose left or right, the first Test, and then according to left left right right side of the right four nodes to make decisions.
PS2: This problem can also be used to conduct violent search DFS in DFS in the process of the problem can not use backtracking, because all solutions are feasible solutions, we are looking for the optimal solution, the optimal solution is generally thought to use branch boundary method to prune, but this problem node value is not the order (unlike the Egyptian score), So it's not possible. Even if you can, there will be repeated calculations.
"Algorithmic Learning Notes" 31. Dynamic planning SJTU OJ 1320 Numtri