// bbb.h //
#include<iostream>
using namespace std;
typedef struct
{
int * elem;
int front;
int rear;
int queuesize;
}sqQueue;
void init_sq(sqQueue &q, int maxsize)
{
q.elem=new int[maxsize-1];
q.queuesize=maxsize;
q.front=q.rear=0;
}
bool delete_sq(sqQueue &q , int & e)
{
int m=0;
for(int i=0;i<9;i++)
m+=q.elem[i];
if(m==0)
return false;
e=q.elem[q.front];
q.elem[q.front]=0;//帮助下一步判断队列是否为空
q.front=(q.front+1)%q.queuesize;
return true;
}
void enter_sq(sqQueue &q , int e)
{
q.elem[q.rear]=e;
q.rear=(q.rear+1)%q.queuesize;
}
bool empty_sq(sqQueue &q)
{
int m=0;//循环队列不能有"q.front==q.rear"来判断队列是否为空
for(int i=0;i<9;i++)
m+=q.elem[i];
if(m!=0)
return false;
else
return true;
}
// bbb.cpp //
#include"bbb.h"
void division(int R[9][9], int n, int result[9])
{
int clash[9]={0,0,0,0,0,0,0,0,0};
sqQueue q;
int pre=n, group=0,l=0;
init_sq(q, n);
for(int k=0;k<n;k++)
enter_sq(q , k);
while(!empty_sq(q))
{
int i;
delete_sq(q , i);
if(i<=pre)//"i==pre"是专门为了最后一个出队的元素而设的
{
group++;
for(int j=0;j<n;j++) clash[j]=0;
}
if(clash[i]==0)
{
result[i]=group;
for(int j=0;j<n;j++)clash[j]+=R[i][j];
}
else
enter_sq(q , i);
pre=i;
}
}
void main()
{
int R[9][9]={
{0,1,0,0,0,1,0,0,0},
{1,0,0,0,1,1,0,1,1},
{0,0,0,0,0,1,1,0,0},
{0,0,0,0,1,0,0,0,1},
{0,1,0,1,0,0,1,0,1},
{1,1,1,0,0,0,1,0,0},
{0,0,1,0,1,1,0,0,0},
{0,1,0,0,0,0,0,0,0},
{0,1,0,1,1,0,0,0,0},
};
int result[9]={0,0,0,0,0,0,0,0,0},sport[9];
division(R, 9, result);
cout<<"result is:"<<endl;
for(int i=0;i<9;i++)
cout<<result[i]<<" ";
cout<<endl<<endl;
cout<<"所有的体育项目为:"<<endl;
for(i=0;i<9;i++)
{
sport[i]=i;//运动项目
cout<<i<<" ";
}
cout<<endl<<endl;
cout<<"运动员报名参加的项目为:"<<endl;
cout<<"(1,4,8) 、"<<"(1,7) 、"<<"(8,3) 、"<<"(1,0,5) 、"<<"(3,4) 、"<<"(5,6,2) 、"<<"(6,4)"<<endl<<endl;;
int k=1;
cout<<"以下排在同一行的体育项目可以同时开展:"<<endl;
while(k<5)
{
cout<<"("<<k<<")"<<"、 ";
for(i=0;i<9;i++)
if(result[i]==k)
cout<<sport[i]<<" ";
cout<<endl;
k++;
}
cout<<endc l;
}