POJ3176-Cow Bowling

Source: Internet
Author: User

 

Reprinted please indicate the source: Thank youHttp://user.qzone.qq.com/289065406/blog/1300453935

General question:

Enter an N-layer triangle. the I-th layer has the number of I. Find the route with the largest sum of weights from layer 1st to layer n.

Rule: a certain number of I layers can only be connected to one of the two numbers adjacent to the position in I + 1.

 

Solution:

The two-dimensional array way [] [] is used to store data in the triangle on the left, so the connection rule is changed

Way [I] [J] → way [I + 1] [J]

OrWay [I] [J] → way [I + 1] [J + 1]

 

Note:Way [] [] is the triangle value at the input time. At this time, way [I] [J] indicates the weight value at the position of the vertex, and the value of the unspecified position is initialized to 0.

 

Solution:

Dynamic Planning.

Way [I] [J] indicates the maximum weight of the route that uses the position of column J of row I as the destination.(Differentiate the meaning of initialization)

Then the maximum value of way [I] [J] depends on way [I-1] [J-1] And way [I-1] [J], from which the maximum value is filtered, add it to way [I] [J], that is, the maximum weight of way [I] [J.

Finally, we only need to compare the weights of all locations in row N [N] [J], and the biggest one is the request.

 

By the way, poj1163 and the two questions are just the same. The difference lies in the value range of N. Therefore, if you apply for a two-dimensional array dynamically, you can copy the code to poj1163 and submit the AC directly.

 

By the way, we can dynamically apply for two-dimensional arrays.

C ++'s new function cannot directly apply for two-dimensional space, but it can be applied indirectly.

Take my program as an example. First, apply for a two-dimensional pointer pointing to a one-dimensional pointer array.

Int ** way = new int * [n + 1];

Every element of this array way [] is a pointer.

Use the for loop to apply for space row by row so that each element (pointer) points to a "one-dimensional array"

Way [I] = new int [I + 2]; // this size is a small optimization determined based on this question. The specific application size varies with the question.

 

These two operations are equivalent to applying for the number of rows of the Two-dimensional array way first, and then applying for the number of columns row by row. This is equivalent to indirectly applying for a two-dimensional array, and the number of columns in each row can be different, optimized the space usage.

 

 

// Memory time // 468 K 172 MS # include <iostream> using namespace STD; int max (int A, int B) {return A> B? A: B;} int main (int I, Int J) {int N; while (CIN> N) {int ** way = new int * [n + 1]; // dynamically apply the first dimension of a two-dimensional array. Each element is a pointer to a one-dimensional array/* input & initial */for (I = 0; I <= N; I ++) {way [I] = new int [I + 2]; // dynamically apply for the second-dimensional array, the space of each row for (j = 0; j <= I + 1; j ++) way [I] [J] = 0; // you cannot use memset to initialize if (I! = 0) for (j = 1; j <= I; j ++) CIN> way [I] [J];}/* DP */INT max_weight = 0; for (I = 1; I <= N; I ++) for (j = 1; j <= I; j ++) {way [I] [J] + = max (way [I-1] [J-1], way [I-1] [J]); if (I = N & max_weight <way [I] [J]) max_weight = way [I] [J] ;}cout <max_weight <Endl; delete [] way;} return 0 ;}

 

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.