(Original) Dynamic Programming Exercise (source hdu 1466, calculate the intersection points of straight lines)

Source: Internet
Author: User

Exercise dynamic planning when you are free. The question used for exercise is the 1466 question of Avionics. The link is as follows:

 

Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1466

Note that this question is the number of intersection solutions. That is to say, if the number of straight lines is given, the intersection points of these straight lines are listed.

First, n straight lines can be arranged and combined, and a maximum of two intersections can be C (n, 2) = n (n-1.

 

Subproblem Division:

M straight lines are divided into two parts. One part is A. The cables in A are parallel to each other, and the other part is B. If B contains r straight lines, the intersection is unknown, it may be partially intersection, partially parallel, but B never has A line parallel to.

In this way, the intersection points of m straight lines = (m-r) the intersection points of parallel lines and r straight lines + the intersection points of r straight lines

(1) Note that the intersection points between (m-r) parallel lines and r straight lines are very good, because the intersection points inside the r straight line are not considered here, therefore, all the intersections must be on the m-r parallel lines, and because there is no parallel line between the r and m-r lines, for each of the m-r lines, it has a straight line intersection with r, so there are (m-r) * r intersections. This value is determined when m and r are determined.

(2) There are more than one solution for the intersection points of r straight lines, but note that the calculation method of these intersection points is the same as that of m straight lines, so it is a sub-problem, and Recursion can be written.

Recursive Function Definition:

Define int func (int lines, int nodes)

Lines indicates the number of straight lines at this time, nodes indicates the number of intersection points, and when the return value of func is 1, it indicates that the number of intersection points of these lines is nodes, and vice versa.

Because there are many recursive duplicates, the rec [m] [n] is used to remember the value of func (m, n) to avoid repeated operations.

How do I calculate the value of func? If m straight lines have n intersections, the intersection points of n = (m-r) * r + r straight lines

In this case, the intersection points of r lines are n-(m-r) * r, that is, the intersection points of r lines are n-(m-r) * r, that is to say, func (r, n-(m-r) * r) = 1

 

In this way, the pseudo code of func (lines, nodes) is as follows:

 

Func (lines, nodes)

{

 

If rec [lines] [nodes] has been assigned a value.

Return rec [lines] [nodes]

For (r = lines; r> 0; r --){

Func (I, nodes-(lines-r) * r );

If the values returned by these func functions are greater than 0

Return 1;

Rec [lines] [nodes] = 1;

Else

Return 0;

 

Rec [lines] [nodes] = 0;

 

}

}

The Code is as follows (implemented in C ++ ):

1 # include <memory. h>
2 # include <iostream>
3 using namespace std;
4
5 int rec [21] [191];
6
7 int func (int lines, int nodes ){
8 if (rec [lines] [nodes]>-1)
9 return rec [lines] [nodes];
10 int r = lines-1, sum = 0;
11 for (; r> 0; r --){
12 int n = nodes-(lines-r) * r;
13 if (n> = 0) sum + = func (r, n );
14}
15 rec [lines] [nodes] = (sum> 0? 1: 0 );
16 return rec [lines] [nodes];
17}
18
19 int main (){
20 memset (rec,-1, sizeof (rec ));
21 int I = 1, j = 0;
22 for (I = 0; I <21; I ++)
23 rec [I] [0] = 1;
24 for (I = 1; I <191; I ++)
25 rec [1] [I] = 0;
26 int numLines = 0;
27 while (cin> numLines ){
28 cout <"0 ";
29 for (j = 1; j <= numLines * (numLines-1)/2; j ++)
30 if (func (numLines, j)> 0)
31 cout <"" <j;
32 cout <endl;
33}
34 return 0;
35}

 

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.