Data tower problems and Simple Dynamic Planning Algorithms

Source: Internet
Author: User

/* <Br/> data tower problems: <br/> 9 <br/> 12 15 <br/> 10 6 8 <br/> 2 18 9 5 <br/> 19 7 10 4 16 <br/> tangible maxcompute, starting from the top, you can choose to go left or right at each node. <br/> keep walking to the bottom layer and find a path to maximize the value of the path. <Br/> If the enumeration method is used for this question, the number of paths to be listed will be a very large number when the number of floors in a few towers is relatively large (for example, 40. <Br/> If greedy is used, the optimal solution is often not obtained. <Br/> you can perform top-down analysis and bottom-up calculation when using dynamic planning to consider the data tower problem. <Br/> whether to go left or right from the vertex depends on whether the maximum value can be obtained from the left or the maximum value can be obtained from the right, <br/> A decision can be made only when the maximum value on the left and right paths is obtained. <Br/> In the same sense, the direction of the next layer depends on whether the maximum value on the next layer has been obtained before decision-making. <Br/> This layer is pushed down until the last layer is very clear. <Br/> for example, if the number is 2, you only need to select the 19 lower node under it. <Br/> when solving the problem, you can start from the bottom layer, step by step, and finally obtain the maximum value. <Br/> conclusion: This is the most basic dynamic planning question. The Division of stages and States is clear at a glance. <Br/> the Decision-Making record fully reflects the essence of dynamic planning, that is, "memory-based search. <Br/> */<br/> # include <iostream> <br/> # define max 20 <br/> using namespace STD; <br/> int main () <br/>{ <br/> cout <"Please input N (lines)" <Endl; <br/> int N; <br/> CIN> N; <br/> int A [Max + 1] [Max + 1] [3]; // [0] is used to store the number. [1] is involved in the operation, [2] indicates left (0) or right (1) <br/> // input data tower <br/> for (INT I = 1; I <= N; ++ I) <br/>{< br/> cout <"Please input line" <I <Endl; <br/> for (Int J = 1; j <= I; ++ J) // Number of I records in row I <br/>{< br/> CIN> A [I] [J] [0]; <br/> A [I] [J] [1] = A [I] [J] [0]; <br/> A [I] [J] [2] = 0; <br/>}< br/> cout <Endl; <br/> // calculate <br/> for (INT I = n-1; I> = 1; -- I) // start from the last row <br/> {<br/> for (Int J = 1; j <= I; j ++) <br/> {<br/> if (a [I + 1] [J] [1]> A [I + 1] [J + 1] [1]) // large on the left <br/>{< br/> A [I] [J] [2] = 0; // select the left <br/> A [I] [J] [1] + = A [I + 1] [J] [1]; <br/>}< br/> else // large on the right <br/>{< br/> A [I] [J] [2] = 1; // select <br/> A [I] [J] [1] + = A [I + 1] [J + 1] [1] on the right. <br/>}</P> <p> // output data tower <br/> for (INT I = 1; I <= N; ++ I) <br/>{< br/> for (Int J = 1; j <= I; ++ J) <br/> {<br/> cout <A [I] [J] [0] <""; <br/>}< br/> cout <Endl; <br/>}< br/> // maximum output value <br/> cout <A [1] [1] [1] <Endl; <br/> // output path <br/> for (INT I = 1, j = 1; I <= N; ++ I) <br/>{ <br/> cout <"[" <I <"," <j <"]" <"-> "; <br/> J + = A [I] [J] [2]; <br/>}< br/> cout <Endl; <br/> return 0; <br/>}

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.