Dynamic Planning-data tower problems and Dynamic Planning

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.