Round robin Calendar
I. Problem-Describing
There are n=2^k athletes, to play a tennis round robin. Now to design a match schedule that meets the following requirements
(1). Each player must match one of the other N-1 players
(2). Each player can only race once a day
(3). Round robin for a total of n-1 days
Two. Problem analysis
According to this request, the contest schedule can be designed into a table of N-line n-1, and in the table I and J are filled in with the opponents of the first player on the J Day.
For example, when the number of players is 8, the race schedule is
algorithm Analysis: according to the Divide and conquer strategy, we can divide all the players into two halves, then the game schedule of N players can be determined by N/2 the game schedule of the contestants. By recursively dividing the contestants with this two-in-one strategy, the game schedule is made simple until there are only two players left. Just let the two contestants play the game. For example, the table of squares listed is the 8-player tournament schedule. The upper left corner and the lower left corner of the two small pieces for the player 1 to 4 and 5 to the player 8 the first 3 days of the game schedule. Accordingly, all the numbers in the upper left corner are copied to the lower right corner by their relative position, and all the numbers in the lower left corner are copied to the upper right corner according to their relative position, so that we have arranged the contestants 1 to 4 and 5 to 8 for the last 4 days of the game schedule. In this way, it is easy to extend the game schedule to a situation with any number of players.
Algorithm implementation steps:
(1) When k=1, that is, the number of 2 people, this situation is the simplest case
This table is
1 2
2 1
(2) When k=2, the number of people is 4, the circular table is
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
(3) When k=3, the number of people is 8, at this time the circular table is
1 2 3 4 5 6 7 8
2 1 4 3 6 5 8 7
3 4 1 2 7 8 5 6
4 3 2 1 8 7 6 5
5 6 7 8 1 2 3 4
6 5 8 7 2 1 4 3
7 8 5 6 3 4 1 2
8 7 6 5 4 3 2 1
And so on, we are not difficult to find, we can use the method of division to achieve, now from top to bottom decomposition, until decomposition to the simplest case, that is, the number of people is 2, then you can 22 games, the table is filled with a diagonal fill of the way, and then fill the table from the bottom up, specifically, see above the k=1,k=2,k= The circular table formed at 3 is well understood.
Three. Source code display
1#include <stdio.h>2#include <math.h>3 #defineN 504 voidGametable (intKintarray[][n]);5 voidPrintintKintArray[][n]);//output two-dimensional arrays6 Main ()7 {8 intK;9 intArray[n][n];Tenprintf"\t\t****************************************\n"); Oneprintf"\t\t**\t\t round robin Calendar **\n"); Aprintf"\t\t****************************************\n\n"); -printf"The number of contestants is N (n=2^k), please enter the value of K:"); - Do the { -scanf"%d",&k); - if(k!=0) - { + gametable (k,array); - print (k,array); + } A Else atprintf"you entered the wrong data, please re-enter"); -} while(k!=0); - - } - voidGametable (intKintArray[][n])//array subscript starting from 1 - { in inti,j,s,t; - intn=1; to for(i=1; i<=k;i++) +n*=2;//total number of people seeking - for(i=1; i<=n;i++) thearray[1][i]=i;//first row row 1-8 * intm=1;//used to control the start fill position of the I row J column at each fill $ for(s=1; s<=k;s++)//s refers to the total number of cycles of symmetric assignment, which is divided into several strides to make a schedulePanax Notoginseng { -n=n/2; the for(t=1; t<=n;t++)//T indicates the number of loops for internally symmetric assignment + for(i=m+1; i<=2*m;i++) A for(j=m+1; j<=2*m;j++) the { +array[i][j+ (t1) *m*2]=array[i-m][j+ (t1) *m*2-M];//The upper -right corner equals the value in the upper-left corner -array[i][j+ (t1) *m*2-m]=array[i-m][j+ (t1) *m*2];//The lower-left corner equals the value in the upper-right corner $ } $m*=2; - } - the } - voidPrintintKintArray[][n])Wuyi { the inti,j; - intNum=pow (2, k); Wuprintf"the round robin schedule for%d people is as follows \ n", num); - for(i=1; i<=num;i++)//output two-dimensional arrays About { $ for(j=1; j<=num;j++) - { -printf"%d\t", Array[i][j]); - } Aprintf"\ n"); + } the}
Four. Program Results display
The round robin schedule of the algorithm