Title description
There are 2 ^ n (n <= 6) teams playing single-cycle games, which are planned to be completed within 2 ^ n–1 days, and each team plays one game every day. Design a game schedule so that each team competes with different opponents within 2 ^ n–1 days.
Input
A line, enter the value of n.
Output
There are a total of (2 ^ n – 1) lines. The ith line outputs the schedule of the ith day.
The format is: <i> AB CD where i is the number of days, A and B are the numbers of the two sides of the game, each row has a total of 2n-1 matches.Sample input
2Sample output
<1>1-2 3-4 <2>1-3 2-4 <3>1-4 2-3
Original title link: [Blue Bridge Cup] [Algorithm Training VIP] Competition Arrangement
Idea:
Take n = 3 as an example, a total of 8 people, 7 days, then the first column must be 1 and 7 other people.
The next two have not played in groups of two, but note that if the two of them have played a few days ago, do not arrange for them to play together, so use the viss array to record who played with whom .
[Adopt the idea of linked list traversal, but judge directly here]
Description of two vis array variables:
name | effect |
---|---|
vis[100] | Throughout the line , vis [j] indicates that j has been hit this day, please do not arrange him |
all [100] | Throughout the entire program , viss [i] [j] indicates that "ij" has been hit, please do not arrange them together |
Attached code:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
int vis[100];
int viss[100][100];
int main(){
int n;
cin>>n;
int per = pow(2,n);
int day = pow(2,n)-1;
for(int i=1;i<=day;i++){
memset(vis,0,sizeof(vis));
cout<<"<"<<i<<">";
int p=1;
while(p<=per){
if(vis[p]==1){ //找出第一个还没打过的p
p++;
continue;
}
int tmp=p;
for(int q=p+1;q<=per;q++){ //判断谁和p打:找出q
if(vis[q]==0 && viss[p][q]==0){ //如果他们之前没打过
vis[p]=vis[q]=viss[p][q]=1;
cout<<p<<"-"<<q<<" ";
p=tmp+1; //判断下一个人p
break;
}
}
}
cout<<endl;
}
return 0;
}