Chapter VII of the divide and conquer algorithm -1325: [Example 7.4] cycle race schedule

1325: [Example 7.4] cycle game schedule
[Title] Description
has NN circulating a player game, where N = 2M2M, requires each player to the other N-1N-1 players have a match, each player per day a competition, a total round robin N-1N-1 day player requires no daily bye.

[INPUT]
Input: MM.

[Output]
Output: competition schedule in tabular form. Separated by a space between each row of data.

[Input] Sample
3
[] sample output
. 1. 4. 5. 6. 7 3 2. 8
2 3. 1. 4. 5. 6. 7. 8
3. 4. 5. 6. 8. 1 2. 7
. 4 3 2. 6. 7. 8. 5. 1
. 5. 6. 7. 8. 1 2 3 . 4
. 6. 7. 8 2. 5. 4. 3. 1
. 7. 8. 6. 5. 4. 3. 1 2
. 8. 6. 7. 5. 4. 3. 1 2
----------------
ideas: split table for the center point, the table is divided into four sections, the left upper = lower right, upper right = lower left;
after all the cells in the top left digital +4 (n / 2), = lower right.
The various parts and then split again conform to the above rules -> 1x1-> 2x2-> 4x4-> 8x8;

#include<cstdio>
#include<iostream>
using namespace std;
const int N = 1030;
int a[N][N]={1};
int main(){
	int m,half = 1;
	cin>>m;
	int n=1<<m;//相当于2^m 
	for(int k = 1;k <= m;k++)
	  {
	  	for(int i = 0;i < half;i++)
	  	 for(int j = 0;j < half;j++)
	  	  {
	  	  	a[i][j + half] = a[i][j] + half;
	  	  	a[i + half][j] = a[i][j + half];
	  	  	 a[i + half][j + half] = a[i][j];
			}
			half *= 2;
	   } 
	for(int i =0 ;i < n;i++){
	
	  for(int j = 0;j < n;j++)
	   cout<<a[i][j]<<" ";
	   cout<<endl;
}	
	return 0;
} 
Published 108 original articles · won praise 2 · Views 2060

Guess you like

Origin blog.csdn.net/zqhf123/article/details/104429250