"Design and Analysis of Algorithms" - round robin schedule

1. Overview: In fact, very much a place of divide and conquer, divide and conquer, recursive solving can help us improve the efficiency of problem-solving

 2. Example: n = k 2 with a round robin of power athlete to be tennis, shooting competitions now need to schedule a meeting the following requirements:

(1) Each player must again with the n-1 other players of each game;

(2) Each player race day only twice:

(3) for a total of n-1 round robin days;

      3, the solution: the schedule in accordance with this requirement may be shot into the game with an n-rows and n-1 columns Table. In Table i-th row and j-th column of the i-th player players filled in the j-th day encountered.

According to the method of divide and conquer strategy, all players can be divided into two halves, one player game schedule n can be determined by competition schedule for the n / 2 players design. Recursive use this strategy into two pairs of players were divided, until only two players when the tournament schedule specified becomes very simple, this time as long as two players to compete on it.

Snippet:

public static void table(int k,int[][] arr){

int n = 1;
for(int i = 1;i<=k;i++){
    n*=2;
}
for(int i = 1;i<=n;i++){
arr[1][i] = i;
}
int m = 1;
for(int s = 1;s<=k;s++){
n/=2;
for(int t = 1;t<=n;t++){
for(int i = m+1;i<=2*m;i++){
for(int j = m+1;j<=2*m;j++){
arr[i][j+(i-1)*m*2] = arr[i-m][j+(t-1)*m*2-m];
arr[i][j+(t-1)*m*2-m] = arr[i-m][j+(t-1)*m*2];
m*=2;
}
}
}
}











}

 

Guess you like

Origin blog.csdn.net/qq_40903237/article/details/94558499