Introduction to the course competition of garlic------the process of the tower problem (DP)

Source: Internet
Author: User

Remember the tower question mentioned in the previous section? Let's work it out together next.
9
12 15
10 6 8
2 18 9 5
19 7 10) 4 15
The above picture is an example of a tower problem. Each time from the top element, that is, 9, each can go to the next adjacent to the two nodes, such as from 9 down adjacent to 12 and 15,6 down adjacent is 18 and 9. Find a path from the top to the bottom to make the number and maximum on the path.


An intuitive greedy strategy is to choose the larger one each time it goes down, and one solution is 9+15+8+9+10=51, but we find that the optimal solution is 9+12+10+18+10=59, which means the problem is not suitable for greedy strategy.


Next we break down the problem, and if we know the optimal solution from the vertex to each point, the final answer can be reached. Assuming that the optimal solution for the J element of line I is f[i][j], it can be thought that f[i][j] is only related to F[i-1][j] and f[i-1][j-1] (if any). This means that the solution of line I is only related to one or two elements of line i-1.

Specifically how to talk about our next, we first follow this idea to write the previous code (つω ' *)

1#include <iostream>2#include <cstdio>3 using namespacestd;4 Const intN = -;5 //The following function implements the update maximum value, O and the maximum value of O and x6Template <classT>7 voidUpdatemax (t& o,Constt&x) {8o = (o > x)?o:x;9 }Ten  One //The f array is a dynamically planned state array A //num Array is the number of reads in the tower - //N is the number of read-in Tower Heights - intF[n][n], num[n][n], N; the  - intMain () { -     //read N and array num -scanf"%d", &n); +      for(inti =1; I <= N; ++i) { -          for(intj =1; J <= I; ++j) { +scanf"%d", &num[i][j]); A         } at     } -  -     //Step 1 Begin: Implementing dynamic programming algorithm logic here -      for(intI=1; i<=n;++i) -     { -          for(intj=1; j<=i;++j) in         { -Updatemax (F[i][j],max (f[i-1][j],f[i-1][j-1])+num[i][j]); to         } +     } -     //Step 1 end. the  *     //defines the final result variable result because it is the maximum value, so it is initialized to 0 $     intresult =0;Panax Notoginseng      for(inti =1; I <= N; ++i) { -         //Step 2 begin: The logic for updating the final result here the Updatemax (Result,f[n][i]); +         //Step 2 end. A     } the     //output final maximum weight and result +printf"%d\n", result); -     return 0; $}

Introduction to the course competition of garlic------the process of the tower problem (DP)

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.