Round robin schedule
There are players to play a tennis round robin. Design a competition schedule that meets the following criteria:
Each player must play other players once each;
Each player can only race once a day;
When is even, the round robin is played for days.
When is odd, the round robin is played for days.
Analysis
When n is a power of two
When , it is easy. We can divide players into 2 parts. The schedule of players can be done by the schedule of . Perform the split recursively until there are only two players left, and the schedule is simple: just let the two players play each other. Then the solution of the original problem can be obtained by gradually merging the solutions of the subproblems.
void tourna(int n) //基本的分治算法 |
tips: means the rival that the th player meet in day .
When n is not a power of two
Let’s talk about what happens when n is not a power of two.
We find that when n is odd, there must be one team empty every day. At this point we assume that there is another non-existent team playing against the empty team, unifying our model for the Odd-even case. The schedule for is similar to the schedule for even .
For example, when :
0 | 1 | 2 | 3 |
---|---|---|---|
1 | 0 | 3 | 2 |
2 | 3 | 0 | 1 |
3 | 2 | 1 | 0 |
When
0 | 1 | 2 | / |
---|---|---|---|
1 | 0 | / | 2 |
2 | / | 0 | 1 |
/ | 2 | 1 | 0 |
To sum up, when n is odd, we can convert it to even.
Next we have the difficult problem of combining matrices.
When is even, it is like when
When is odd, I referenced 猪一戒的博客
Let’s think about what happens when n is equal to 6.
We divide six people into two groups of three ([0,1,2],[3,4,5]), and then find that three is an odd number. Then we add one dummy in each group: X and Y; So you have four people in each group, and you divide those four people by two, and you get a small group of pairwise pairs.
Firstly, we see [0,1]; [2,x]
0 | 1 |
---|---|
1 | 0 |
2 | x |
---|---|
x | 2 |
Combine these two:
0 | 1 | 2 | x |
---|---|---|---|
1 | 0 | x | 2 |
2 | x | 0 | 1 |
x | 2 | 1 | 0 |
Here, we want to get a match arrangement of 3 players, so we drop the imaginary X and replace its position with / :
0 | 1 | 2 | / |
---|---|---|---|
1 | 0 | / | 2 |
2 | / | 0 | 1 |
Then we also follow this rule, schedule [3,4,5].
3 | 4 | 5 | / |
---|---|---|---|
4 | 3 | / | 5 |
5 | / | 3 | 4 |
We have two 3x4 matrices (where the first column represents each team, which is really only three days), and we end up with a 6 by 6 matrix (where the first column represents each team, which is really only five days).
So let’s just merge the top two matrices.
0 | 1 | 2 | / | ||
---|---|---|---|---|---|
1 | 0 | / | 2 | ||
2 | / | 0 | 1 | ||
3 | 4 | 5 | / | ||
4 | 3 | / | 5 | ||
5 | / | 3 | 4 |
The races for the first three days are almost finished, we just need to fill in the corresponding races in the slash /. Obviously, you could have two teams playing each other every day. (There are no slashes in the program, or imaginary teams, for easy monitoring)
0 | 1 | 2 | 3 | ||
---|---|---|---|---|---|
1 | 0 | 4 | 2 | ||
2 | 5 | 0 | 1 | ||
3 | 4 | 5 | 0 | ||
4 | 3 | 1 | 5 | ||
5 | 2 | 3 | 4 |
In the matrix above, [0,1,2] and [3,4,5] have been compared within the group, and the group has been compared once, and the rest only need to rotate twice to get the match situation of the next two days.
0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
1 | 0 | 4 | 2 | 5 | 3 |
2 | 5 | 0 | 1 | 3 | 4 |
3 | 4 | 5 | 0 | 2 | 1 |
4 | 3 | 1 | 5 | 0 | 2 |
5 | 2 | 3 | 4 | 1 | 0 |
So we can get the combination of two odd matrices.
In the program, our thinking is not strictly “merge”, but “extend”. For example, for “merging two 3x4 matrices”, we are “expanding one 3x4 matrix into a 6x6 matrix” (because the two 3x4 matrices have the same size and the same sorting order, so we only need to do it once).
Code
#include<stdio.h> |
Test
The test program checks the matrix we generated. There are two ways to do it.
Each team will play a match against the other teams
Each team will play only one match per day
The test program code is as follows.
int Check(int **a, int n) |
Result
- When
- When
They are all correct.