Algorithm brainwashing series (8)-Part 5 divide and conquer Thoughts

Source: Internet
Author: User

As my recent work is busy and I haven't updated my blog for a long time, I will share my thoughts on governance.

 

I. Thoughts

Sometimes we deal with a complicated problem. It may be because the steps for solving the problem are complicated or there may be a lot of data, which makes it difficult or impossible for us to find the problem at that time. The old saying goes:

This idea lies inAlgorithmThe idea of sub-governance is that we can break down the problem into several sub-problems, solve the sub-problems one by one, and finally resolve the sub-problems.

The answer is combined into the answer to the entire question.

 

Ii. Conditions

Of course, every idea has its own field of use. Therefore, to play this sub-governance game, we must abide by its game rules.

① Solving problems can indeed be divided into several smaller subproblems, and these subproblems can finally be solved in close proximity or O (1) time.

② There is no dependency between each subproblem, and these subproblems can be combined to obtain the entire problem solution.

 

Step 3:

Based on the preceding description of sub-governance, we can see that sub-governance follows three steps:

① Decomposition: the problem is divided into several small problems.

② Solving: O (1) solves this subproblem.

③ Merge: Merge sub-questions one by one to form a solution to the entire problem.

 

Iv. Example

There are n contestants participating in the badminton match. The match takes n-1 days. Each contestant has to compete with each other and each contestant has to compete every day. Please follow the competition requirements.

Discharge the contestant's competition schedule.

 

Idea: First of all, we need to cheer ourselves up to solve the problem. Haha, because basic daily problems do not come out of the scope of what we know about algorithm ideas. This problem is also included.

When N is 8, 16, and 32, we may crash in the face of such a huge problem, because we can't find it. At this point, we need to think about whether it can be managed separately.

① Taking the Competition Arrangement of 16 players as an example, it takes 15 days for the competition.

② Divide the competition into two eight contestants for seven days.

③ The competition is divided into four contestants for three days.

④ There are eight two contestants in one day.

I believe that the two contestants will make a one-day competition ,:

Then it degrades to step 3, that is, the three-day competition schedule of four contestants,

The figure shows that:

Day 1: Merge the schedules of two or three contestants.

Day 2 and Day 3: regular patterns can be found during the two-day competition. The entire table can be divided into four cells and assigned a value on the diagonal.

ProgramCodeAs follows:

 1   Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5
6 Namespace Fenzhi
7 {
8 Public Class Program
9 {
10 // Here
11 Static Int [,] Gamelist = New Int [ 8 , 8 ];
12
13 Static Void Main ( String [] ARGs)
14 {
15 Console. Write (" Enter the number of contestants: \ t " );
16
17 Int Person = convert. toint32 (console. Readline ());
18
19 // Parameter check validity
20 If (Person % 2 ! =0 )
21 {
22 Console. writeline ( " The number of people entered must be a multiple of 2! " );
23 Return ;
24 }
25
26 // Because I have set a schedule to accommodate only eight contestants, if there are more than one schedule, it will crash.
27 If (Person> 8 )
28 {
29 Console. writeline ( " Sorry, up to 8 contestants " );
30 Return ;
31 }
32
33 // Call the score calculation function
34 Gamecal ( 1 , Person );
35
36 Console. Write ( " \ N no. \ t " );
37
38 // Finally, the array Header
39 For ( Int I = 1 ; I <person; I ++)
40 {
41 Console. Write ( " Day {0} \ t " , I );
42 }
43
44 // Line feed
45 Console. writeline ();
46
47 // Output array content
48 For ( Int I = 0 ; I <person; I ++)
49 {
50 For ( Int J = 0 ; J <person; j ++)
51 {
52 Console. Write ( " {0} \ t " , Gamelist [I, j]);
53 }
54 Console. writeline ();
55 }
56
57 Console. Readline ();
58 }
59
60 /// <Summary>
61 /// Divide and conquer computing
62 /// </Summary>
63 /// <Param name = "Index"> Start contestant ID </Param>
64 /// <Param name = "num"> The number of contestants (because it is divided, so each cut half) </Param>
65 Static Void Gamecal ( Int Index, Int Num)
66 {
67 // If the number of students is 2, it indicates that the sub-division has reached the simplest problem.
68 If (Num = 2 )
69 {
70 // Contestant ID
71 Gamelist [index- 1 , 0 ] = Index;
72
73 // Competitor ID
74 Gamelist [index- 1 ,1 ] = Index + 1 ;
75
76 // Contestant ID
77 Gamelist [index, 0 ] = Index + 1 ;
78
79 // Competitor ID
80 Gamelist [index, 1 ] = Index;
81 }
82 Else
83 {
84 // Half-recursion
85 Gamecal (index, num/ 2 );
86
87 // Half-recursion
88 Gamecal (index + num/ 2 , Num/ 2 );
89
90 /* After all the sub-problems are completed, we need to find a way to merge them based on the discovered rules. */
91
92 // Fill "lower left corner" to "upper right corner"
93 // Control horizontal coordinates
94 For ( Int I = index; I <index + num/ 2 ; I ++)
95 {
96 // Control "ordinate"
97 For ( Int J = num/ 2 ; J <num; j ++)
98 {
99 // Diagonal assignment
100 Gamelist [I- 1 , J] = gamelist [(I- 1 ) + Num/2 , J-num/ 2 ];
101 }
102 }
103
104 // Fill "upper left corner" to "lower right corner"
105 // Control horizontal coordinates
106 For (Int I = index + num/ 2 ; I <index + num; I ++)
107 {
108 // Control ordinate
109 For ( Int J = num/ 2 ; J <num; j ++)
110 {
111 // Diagonal assignment
112 Gamelist [I- 1 , J] = gamelist [(I- 1 )-Num/ 2 , J-num/ 2 ];
113 }
114 }
115 }
116 }
117 }
118 }

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.