Number of towers
Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 34230 Accepted Submission (s): 20423
Problem description When it comes to DP algorithms, a classic example is the tower problem, which is described in this way:
As shown in the following tower, required to go from the top floor to the bottom, if each step can only go to adjacent nodes, then the number of nodes through the sum of the maximum is how much? Already told you, this is a DP problem, can you AC? Input data first includes an integer c, which indicates the number of test instances, the first line of each test instance is an integer N (1 <= N <= 100), which represents the height of the tower, followed by the number of columns in N rows, where I is an integer in the line I, and all integers are in the interval [ 0,99] inside. Output for each test instance, the outputs may be the largest and one row per instance output. Sample Input1573 (1 0 2 7 4) 5 2 6 5 Sample Output30
1#include <iostream>2 using namespacestd;3 intreward[ -][ -], best[ -][ -];4 intMain () {5 inttest, N;6CIN >>test;7 8 while(test--){9CIN >>N;Ten One for(inti =0; I < n; i++) A for(intj =0; J <= I; J + +) -CIN >>Reward[i][j]; - thebest[0][0] = reward[0][0]; - - for(inti =1; I < n; i++){ - for(intj =0; J <= I; J + +) { + if(J = =0) -BEST[I][J] = best[i-1][J] +Reward[i][j]; + Else if(i = =j) ABEST[I][J] = best[i-1][j-1] +Reward[i][j]; at Else -BEST[I][J] = (best[i-1][J] > best[i-1][j-1] ? best[i-1][J]: best[i-1][j-1]) +Reward[i][j]; - } - } - - intmax = best[n-1][0]; in for(inti =1; I < n; i++){ - if(best[n-1][i] >max) tomax = best[n-1][i]; + } - thecout << Max <<Endl; * } $ //System ("pause");Panax Notoginseng return 0; -}
HDU Number Tower