Divide and Conquer – Round Robin Schedule

There are a total of 2^k players, which is exactly an integer power of 2. Each player can only compete once a day and must compete with other players once, for a total of n-1 days. As shown in the figure, with the number of days as the abscissa and the athlete number as the ordinate, we get a competition schedule.

It is not difficult to find that the red box in the upper left corner of the picture is the same as the red box in the lower right corner, and the blue box in the lower left corner is the same as the blue box in the upper right corner. In other words, we only need to symmetry from one half to the other half to complete this matrix.

 The most difficult thing to understand here is this symmetrical assignment. In fact, the order of this assignment is very strange, as shown in the figure below

 

 And it’s not enough for me to run it myself. I enter the code and find that the code is completely wrong! m*=2 should be placed in the outer layer of i to loop n times, otherwise there is no need for a t loop in the sample code. Assume k=2, n enters the loop n/2=2, t loops twice, then m*2=2, and next time i loops 2 times, m*2*2=8, which is completely wrong. Moreover, these two loop statements are complicated and difficult to understand.

Upper right matrix = upper left matrix + scale m

The lower left matrix = the upper right matrix is ​​symmetrical, the lower right matrix = the upper left matrix is ​​symmetrical 

	int mid=1;
    while (i <= m)
	{    
        //将矩阵分成左上右上左下右下四块,每块规模都是mid*mid
        //每次进入下一次分治之前左上方的矩阵都已经求出来了
		for (int j = 0; j < mid; j++)//构造右上方方阵,右上方矩阵=左上方+mid
			for (int k = 0; k < mid; k++)
				a[j][k + mid] = a[j][k] + mid;
		for (int j = 0; j < mid; j++)//对称交换构造下半部分方阵
			for (int k = 0; k < mid; k++)
			{ 
				a[j + mid][k] = a[j][k + mid];/*左下方方阵等于右上方方阵*/
				a[j + mid][k + mid] = a[j][k];//右下方方阵等于左上方方阵
			}				
			mid *= 2, i++;
	}

Isn't it much simpler to find the upper left and upper right, and then make it symmetrical? It has to be so troublesome and a waste of time.

Guess you like

Origin blog.csdn.net/milu_ELK/article/details/127019059