Dynamic Planning-data tower problems and Dynamic Planning
Starting from the data tower top layer, each node can choose to go left or right, requiring that it go all the way to the bottom of the tower, so that the value and maximum of the path that has passed.
# Include <iostream> # include <cstdio> using namespace std; const int N = 100; // The following function implements the maximum update value, the maximum template <class T> void updateMax (T & o, const T & x) {o = (o> x) Where o values are o and x )? O: x ;} // The f array is a dynamically planned status array // The num array is the read data tower // n is the read data tower height int f [N] [N], num [N] [N], n; int main () {// read n and the number tower array num scanf ("% d", & n ); for (int I = 1; I <= n; ++ I) {for (int j = 1; j <= I; ++ j) {scanf ("% d", & num [I] [j]) ;}}for (int I = 1; I <= n; I ++) {for (int j = 1; j <= I; j ++) {// use another array to store updateMax (f [I] [j], max (f [I-1] [j], f [I-1] [j-1]) + num [I] [j]) ;}// step 1 begin: here we implement the dynamic planning algorithm logic // step 1 end. // final definition Result variable. The result is initialized to 0 int result = 0 because it calculates the maximum value. for (int I = 1; I <= n; ++ I) {// because the value of the previous loop has been calculated, you can search for it directly at the last layer // step 2 begin: the logic of updateMax (result, f [n] [I]); // assign values instead of exchanging values. Remember !!!!!! // Step 2 end.} // output the final maximum weight and result printf ("% d \ n", result); return 0 ;}
Implementation Code