I. Problem description: N athletes are requiredTennis round robin. Design a calendar meeting the following requirements:
(1) Each contestant must compete with the other n-1 contestants each time;
(2) Each contestant can only compete once a day;
(3) When n is an even number, the round robin starts for n-1 days. When N is an odd number, the round robin lasts for N days.
Ii. Problem Analysis andAlgorithmDesign:
According to the grouping policy, all contestant pairs can be divided into two groups (if n is an even number, it is directly divided into n/2, and if n is an odd number, it is (n + 1) /2 in each group), the schedule of n players can be determined by the schedule designed for (n/2 or (n + 1)/2) players. Recursively split the contestants using this split strategy until only two players are left, the preparation of the competition schedule becomes very simple. In this case, you only need to let the two players compete.
The following table lists the schedules of the six contestants. The first column indicates 1-6 contestants, and the second column to the sixth column indicates the contestants from the first day to the fifth day.
1 2 3 4 5 6
2 1 5 3 6 4
3 6 1 2 4 5
4 5 6 1 3 2
5 4 2 6 1 3
6 3 4 5 2 1
HereAlgorithmThe difficulty of the design is the merger after separate governance. Here I will describe it in combination with the example of the above six contestants. First, divide the six contestants into two peer groups, each of which has three players. Then, recursively divide the three contestants into two peer groups, with two players in each group.
In the case of two players, the two players competed. The following table lists the schedules of the two contestants:
1 2
2 1
The next task is to merge the schedules of the two contestants in the two groups to get the schedules of the three contestants. Here I assume that there are four contestants participating in the competition:
1 2
2 1
3 4
4 3
In the next game, let's have 1 and 3 matches, 2 and 4 matches, and 1 and 4 matches, 2 and 3 matches on the third day, that is, let the players in the previous group, the following table shows the schedule of the round robin and the last group of contestants:
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
Here we will get a schedule for three contestants. Then, if 4th contestants use 0 as the hypothetical contestant, we will get a schedule for the three contestants:
1 2 3 0
2 1 0 3
3 0 1 2
The next task is to merge the schedules of the two three contestants to get the schedules of the six contestants. Here, the first three days of the competition between the two contestants are as follows:
1 2 3 0
2 1 0 3
3 0 1 2
4 5 6 0
5 4 0 6
6 0 4 5
Both contestant 3 and contestant 6 have no opponents on the first day, so they can play two games. Contestant 2 and contestant 5 have no opponents on the second day, so they can play two games ,; on the third day, Contestant 1 and contestant 4 had no opponents and asked them to compete. In this way, we can get the schedule of the first three days of the six contestants after the merger:
1 2 3 4
2 1 5 3
3 6 1 2
4 5 6 1
5 4 2 6
6 3 4 5
List the contestants in the two groups that match each other in the previous three days:
1 2 3
4 5 6
Here we can see that three and six, two, five, one, and four of the two groups have been compared. Here we will skip these contestant competitions, and then the two groups will play in a loop:
1 2 3
5 6 4
And
1 2 3
6 4 5
In this way, the complete schedule of the six contestants is obtained:
1 2 3 4 5 6
2 1 5 3 6 4
3 6 1 2 4 5
4 5 6 1 3 2
5 4 2 6 1 3
6 3 4 5 2 1
Iii. ProofAlgorithmCorrectness:
(1) At n = 2, the two contestants are playing for only one day.AlgorithmInitial Condition,AlgorithmYes.
(2) At n = K, if K is an even number, K players are divided into k/2 two groups, so that according to the problem requirements K players a total of K-1 days, if K/2 players are even numbers, then the game (K/2)-one day, during the merger, the two groups of K/2 players will need K/two days for the loop competition, then the first group and then merge a total of (K/2)-1 + (K/2) = K-1 days; k/2 players if the odd number, then the game K/2 days, during the merger, each contestant in the two groups had a match, so it would take (K/2)-1 day for the two K/2 contestants to go round, then grouping first and then merging a total of (K/2) + (K/2)-1 = K-1 days.
(3) the case where K is an odd number is similar to that where K is an even number.
Reprinted Website: http://www.stubc.com/thread-2645-1-1.html
Code:
- /***
- ** The following functions are from the Wang Xiaodong algorithm book, but they are not explained and cannot be understood at all. You can find the above algorithm analysis on the Internet.
- I drew a picture on the paper and finally figured out what was going on. I sorted out the code in the book and implemented it on vc6.0.
- Post it to share with you
-
- **
- ***/
- # Include <iostream>
- Using namespace STD;
- Const int size = 50;
- Int A [size] [size];
- Void copy (int n );
- Void tournament (int n );
- Bool odd (int n );
- Void makecopy (int n );
- Void copyodd (int n );
- Int main ()
- {
- Int N;
- Int I, J;
- Cin> N;
- Tournament (N );
- If (ODD (N) // if n is an odd number or an even number, consider
- {
- For (I = 1; I <= N; I ++)
- {
- For (j = 1; j <= n + 1; j ++)
- If (A [I] [J] = n + 1)
- Cout <"0 ";
- Else
- Cout <A [I] [J] <"";
- Cout <Endl;
- }
- }
- Else
- {
- For (I = 1; I <= N; I ++)
- {
- For (j = 1; j <= N; j ++)
- Cout <A [I] [J] <"";
- Cout <Endl;
- }
- }
- Return 0;
- }
- /**
- * COPY Copies all the numbers in the small block calculated recursively in the upper left corner to the lower right corner.
- Add n/2 to all the numbers in the upper-right corner and copy them to the lower-left corner.
- Copy all numbers to the top-right corner *
- **/
- Void copy (int n)
- {
- Int M = n/2;
- For (INT I = 1; I <= m; I ++)
- For (Int J = 1; j <= m; j ++)
- {
- A [I] [J + M] = A [I] [J] + m;
- A [I + M] [J] = A [I] [J + M];
- A [I + M] [J + M] = A [I] [J];
- }
- }
- Void tournament (int n)
- {
- If (n = 1)
- {
- A [1] [1] = 1;
- Return;
- }
- If (ODD (n ))
- {
- Tournament (n + 1 );
- Return;
- }
- Tournament (n/2 );
- Makecopy (N );
- }
- Bool odd (int n)
- {
- Return N & 1;
- }
- Void makecopy (int n) // makecopy is similar to the copy algorithm, but n/2 is an odd or even number.
- {
- If (n/2> 1 & odd (n/2 ))
- Copyodd (N );
- Else
- Copy (N );
- }
- Void copyodd (int n) // implement copy when n/2 is an odd number
- {
- Int B [size];
- Int M = n/2;
- For (INT I = 1; I <= m; I ++)
- {
- B [I] = m + I;
- B [M + I] = B [I];
- }
- For (I = 1; I <= m; I ++) // This loop is hard to understand. Try to walk n = 6 on the paper, you can understand why you want to write this code.
- {
- For (Int J = 1; j <= m + 1; j ++)
- {
- If (A [I] [J]> m)
- {
- A [I] [J] = B [I];
- A [M + I] [J] = (B [I] + M) % N;
- }
- Else
- A [M + I] [J] = A [I] [J] + m;
- }
- For (j = 2; j <= m; j ++)
- {
- A [I] [M + J] = B [I + J-1];
- A [B [I + J-1] [M + J] = I;
- }
- }
- }