本文为数据结构与算法课设《田径比赛的时间安排问题》课题的一个分享,使用涂色算法和菜单导航来实现安排最合理的赛程安排。在原先的基础上没有进行大的改动,只做了一些优化。
(*对于C语言的widdows库函数应该了解学习一下)
目录
1.设计目的与要求
2.代码实现
1.结构体定义
2.全局变量和变量定义
3.键入信息或者文件导入
4.打印邻接矩阵
5.着色和涂色算法
6.输出时间安排
7.菜单函数与主函数
8.装饰:开场欢迎和结束
*9.完整代码
1.设计目的与要求
设计内容:
设某田径比赛共有m个比赛项目,规定每个选手至多可参加三个项目,有n人报名参加比赛。设计比赛日程表,使得比赛能在尽可能短的时间内完成。要求:m个比赛项目及人员的报名情况表均事先存储在文件中,需要用数据时从文件中读取。
设计要求:
(1) 符合课题要求,实现相应功能;
(2) 要求界面友好美观,操作方便易行;
(3) 注意程序的实用性、安全性
2.代码实现
1.结构体定义
struct Node {
int number;
int item; //度
int color; //颜色
}node[MAX];
typedef struct Race {
char name[MAX];
}race[9];
结构体定义一个图的结构来实现功能。
2.全局变量和变量定义
using namespace std;
#define OVERFLOW -2 //内存溢出错误常量
#define ILLEGAL -1 //非法操作错误常量
#define OK 1 //表示操作正确的常量
#define ERROR 0 //表示操作错误的常量
#define MAX 20 //最大比赛项目数量
int a[MAX][MAX] = { 0 };
int i, j, x;
FILE* filep;
FILE* f;
int b[3], item, temp;
char c, ch;
bool flag = false;//判断用户是否输入数据
3.键入信息或者文件导入
void DataFromKeyboard()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★你选择的方法是键盘输入★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 本届田径比赛的项目种类 ||\n";
cout << "\t|| [1]110米跑步 [2]100米跑步 [3]跳高 ||\n";
cout << "\t|| [4]1000米长跑 [5]800米长跑 [6]110米跨栏 ||\n";
cout << "\t|| [7]100米跨栏 [8]跳远 [9]铅球 ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 接下来的每一行是该运动员所参加的所有运动项目序号(每人最多参加三项) ||\n";
cout << "\t|| ||\n";
cout << "\t|| 以 0 结束输入 ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
while (c != '0')
{
c = getchar();
if (c == '\n')
{
for (j = 0; j < i - 1; j++)
a[b[j]][b[j + 1]] = a[b[j + 1]][b[j]] = 1;
if (i == 3)
a[b[0]][b[2]] = a[b[2]][b[0]] = 1;
i = 0;
cout << "\t\t\t\t\t";
}
if (c > '0' && c <= '9')
b[i++] = c - 48;
if (c == '0')
break;
}
for (i = 1; i < 10; i++)
{
item = 0;
for (j = 1; j < 10; j++)
{
if (a[i][j])
item++;
}
node[i].item = item;
node[i].number = i;
node[i].color = 0; //0表示初始化无颜色
}
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★数据已成功录入★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t =============================================================================\n";
flag = true;
//着色
Draw(1, 1);
//按颜色排序
qsort(node + 1, 6, sizeof(node[1]), cmp);
cout << "\n\n\n";
cout << "按任意键继续...";
getchar();
getchar();
system("CLS");
}
void DataFromFile()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★你选择的方法是文件导入★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 请确保文件中的格式为 ||\n";
cout << "\t|| 接下来的每一行是该运动员所参加的所有运动项目序号(每人最多参加三项) ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| 是否继续(y或n) ||\n";
cout << "\t||============================================================================||\n";
char op;//记录用户选择
re://用户输入错误
cout << "\t\t\t\t\t请输入:";
cin >> op;
if (op == 'n' || op == 'N')
{
cout << "即将返回主菜单,请稍等...";
Sleep(3500);
system("CLS");
return;
}
else if (op == 'y' || op == 'Y');
else
{
cout << "\t|| 输入错误重新输入 ||\n";
goto re;
}
char FileName[100]; //文件名
cout << "\t||============================================================================||\n";
cout << "\t\t\t\t 请输入文件名称:";
cin >> FileName;
cout << "\t||============================================================================||\n";
f = fopen(FileName, "r");
c = fgetc(f);
//读取文件并输出
if ((filep = fopen(FileName, "r")) == NULL)
{
cout << "\t\t\t打开文件错误!" << endl;
cout << "\t\t\t即将返回主菜单...";
Sleep(3500);
system("CLS");
return;
}
ch = fgetc(filep);
while (ch != EOF)
{
// putchar(ch);
ch = fgetc(filep);
}
//把文件里的数据录入到矩阵里
i = 0;
while (c != EOF)
{
if (c == '\n')
{
for (j = 0; j < i - 1; j++)
a[b[j]][b[j + 1]] = a[b[j + 1]][b[j]] = 1;
if (i == 3)
a[b[0]][b[2]] = a[b[2]][b[0]] = 1;
i = 0;
}
if (c >= '0' && c <= '9')
b[i++] = c - 48;
c = fgetc(f);
}
for (i = 1; i < 10; i++)
{
item = 0;
for (j = 1; j < 10; j++)
{
if (a[i][j])
item++;
}
node[i].item = item;
node[i].number = i;
node[i].color = 0; //0表示初始化无颜色
}
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★数据已成功录入★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t =============================================================================\n";
flag = true;
//着色
Draw(1, 1);
//按颜色排序
qsort(node + 1, 6, sizeof(node[1]), cmp);
cout << "\n\n\n";
cout << "按任意键继续...";
getchar();
getchar();
system("CLS");
}
导入的信息是报名信息,每一行都表示一个同学的报名信息。
4.打印邻接矩阵
void PrintAdjMatrix()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★正在打印邻接矩阵★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
for (int i = 1; i < 10; i++) {
printf("\t\t\t\t ");
for (int j = 0; j < 10; j++) {
printf("%d ", a[i][j]);
}
printf("\n\n");
}
cout << "\t|| ||\n";
cout << "\t =============================================================================\n";
cout << "\n\n\n";
cout << "按任意键继续...";
getchar();
getchar();
system("CLS");
}
本质上是二维数组的打印输出。
5.着色和涂色算法
int cmp(const void* a, const void* b)
{
struct Node* c = (Node*)a;
struct Node* d = (Node*)b;
return c->color - d->color;
}
int Draw(int k, int color) {
// 如果节点k已经被着色,则无需再着色
if (node[k].color != 0) return OK;
node[k].color = color; // 为节点k着色
// 遍历所有与节点k不相连且未着色的节点
for (int j = 0; j < MAX_NODES; j++) {
if (!a[k][j] && node[j].color == 0) {
bool canColor = true;
// 检查与节点j相连的所有节点,确保没有与节点k同色的
for (int i = 0; i < MAX_NODES; i++) {
if (a[j][i] && node[i].color == color) {
canColor = false;
break;
}
}
if (canColor) {
node[j].color = color; // 如果可以着色,则为节点j着色
}
}
}
// 如果还有未着色的节点,递归调用Draw函数
for (int i = 0; i < MAX_NODES; i++) {
if (node[i].color == 0) {
Draw(i, color + 1); // 尝试使用下一个颜色
}
}
return OK;
}
涂色算法:(图着色问题),将当前未着色的点按度数(区别量)降序排列。 将第一个点染成一个未被使用的颜色。 顺次遍历接下来的点,若当前点和所有与第一个点颜色 相同 的点 不相邻 ,则将该点染成与第一个点相同的颜色。 若仍有未着色的点,则回到步骤 1, 否则结束。
初始化:首先,所有顶点都未着色(即颜色为0或某个特殊值)。递归着色:选择一个顶点并尝试为其着色。然后,检查其所有未着色的邻接顶点,并尝试为它们着色,确保没有相邻顶点具有相同的颜色。回溯:如果某个顶点无法用当前颜色着色(因为它与已着色的邻接顶点颜色冲突),则尝试下一个颜色。如果所有颜色都尝试过但仍无法着色,则需要回溯到之前的顶点,并尝试不同的着色方案。终止条件:当所有顶点都被成功着色时,算法终止。
6.输出时间安排
int ShortTime()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★本届田径比赛的安排如下★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| [1]110米跑步 [2]100米跑步 [3]跳高 ||\n";
cout << "\t|| [4]1000米长跑 [5]800米长跑 [6]110米跨栏 ||\n";
cout << "\t|| [7]100米跨栏 [8]跳远 [9]铅球 ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
int day = 1;
printf("\t\t\t第%d天\t%d", day, node[1].number);
temp = node[1].color;
for (i = 2; i < 10; i++) //输出
{
if (temp != node[i].color)
{
printf("\n");
day++;
printf("\t\t\t第%d天", day);
}
printf("\t%d ", node[i].number);
temp = node[i].color;
}
cout << "\n\t|| ||\n";
cout << "\t =============================================================================\n";
cout << "\n\n\n";
cout << "按任意键继续...";
getchar();
getchar();
system("CLS");
return OK;
}
这里最开始的想法是直接输出项目名称,但是不方便输出,就还是以序号输出了。
7.菜单函数与主函数
int Menu()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★田径比赛的时间安排系统★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 本届田径比赛的项目种类 ||\n";
cout << "\t|| [1]110米跑步 [2]100米跑步 [3]跳高 ||\n";
cout << "\t|| [4]1000米长跑 [5]800米长跑 [6]110米跨栏 ||\n";
cout << "\t|| [7]100米跨栏 [8]跳远 [9]铅球 ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 功能选项卡 ||\n";
cout << "\t|| ||\n";
cout << "\t|| 【1】--- 从键盘中输入数据 ||\n";
cout << "\t|| 【2】--- 从文件中导入数据 ||\n";
cout << "\t|| 【3】--- 输出邻接矩阵 ||\n";
cout << "\t|| 【4】--- 输出安排的时间 ||\n";
cout << "\t|| 【5】--- 退出程序 ||\n";
cout << "\t|| ||\n";
cout << "\t ==============================================================================\n";
cout << "\t\t\t请输入数字来选择对应的功能:";
int num;
cin >> num;
return num;
}
int main()
{
Start_Screen();
system("color 7");
while (true)
{
int num = Menu();
switch (num)
{
case 1: {//从键盘中输入数据
system("CLS");
DataFromKeyboard();
break;
}
case 2: {//从文件中导入数据
system("CLS");
DataFromFile();
break;
}
case 3: {//输出邻接矩阵
if (flag != true)
{
cout << "\t|| ||\n";
cout << "\t|| 您还未输入数据,请选择 1 或 2 ||\n";
cout << "\t|| ||\n";
cout << "\t =============================================================================\n\n\n\n";
cout << "正在为您跳转到主菜单,请稍等...";
Sleep(2500);
system("CLS");
continue;
}
system("CLS");
PrintAdjMatrix();
break;
}
case 4: {//输出安排的时间
if (flag != true)
{
cout << "\t|| ||\n";
cout << "\t|| 您还未输入数据,请选择 1 或 2 ||\n";
cout << "\t|| ||\n";
cout << "\t =============================================================================\n\n\n\n";
cout << "正在为您跳转到主菜单,请稍等...";
Sleep(2500);
system("CLS");
continue;
}
system("CLS");
ShortTime();
break;
}
case 5: {//退出程序
system("CLS");
Finish_Screen();
exit(0);
break;
default:
cout << "\n\t\t您输入的序号有误,请重新选择,按任意键继续...";
getchar();
getchar();
system("CLS");
break;
}
}
}
return 0;
}
8.装饰:开场欢迎和结束
void Start_Screen()
{
system("color 5");//将字体变为深蓝色
cout << "\n\n\n";
cout << "\t\t*************************************************" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t****** ★★欢迎进入★★ ******" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t****** 田径比赛的时间安排系统 ******" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t*************************************************" << endl;
cout << "\n\n\n";
cout << "\t\t正在为您跳转到主菜单,请稍等...";
Sleep(3000);
system("CLS");
}
void Finish_Screen()
{
system("color 9");
cout << "\n\n\n\n\n";
cout << "\t\t*************************************************" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t****** ★★谢谢使用★★ ******" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t****** 田径比赛的时间安排系统 ******" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t*************************************************" << endl;
cout << "\n\n\n\n\n";
}
使用system来产生延时等待效果。
*9.完整代码
整体实现并不复杂,主要难点在于理解涂色算法进行时间安排。
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define OVERFLOW -2 //内存溢出错误常量
#define ILLEGAL -1 //非法操作错误常量
#define OK 1 //表示操作正确的常量
#define ERROR 0 //表示操作错误的常量
#define MAX 20 //最大比赛项目数量
struct Node {
int number;
int item; //度
int color; //颜色
}node[MAX];
typedef struct Race {
char name[MAX];
}race[9];
int a[MAX][MAX] = { 0 };
int i, j, x;
FILE* filep;
FILE* f;
int b[3], item, temp;
char c, ch;
bool flag = false;//判断用户是否输入数据
//******************************************自定义函数类*******************************************
int Draw(int k, int color);
int cmp(const void* a, const void* b);
void Start_Screen();
void Finish_Screen();
int Menu();
void DataFromFile();
void DataFromKeyboard();
void PrintAdjMatrix();
int ShortTime();
//******************************************主程序*************************************************
int main()
{
Start_Screen();
system("color 7");
while (true)
{
int num = Menu();
switch (num)
{
case 1: {//从键盘中输入数据
system("CLS");
DataFromKeyboard();
break;
}
case 2: {//从文件中导入数据
system("CLS");
DataFromFile();
break;
}
case 3: {//输出邻接矩阵
if (flag != true)
{
cout << "\t|| ||\n";
cout << "\t|| 您还未输入数据,请选择 1 或 2 ||\n";
cout << "\t|| ||\n";
cout << "\t =============================================================================\n\n\n\n";
cout << "正在为您跳转到主菜单,请稍等...";
Sleep(2500);
system("CLS");
continue;
}
system("CLS");
PrintAdjMatrix();
break;
}
case 4: {//输出安排的时间
if (flag != true)
{
cout << "\t|| ||\n";
cout << "\t|| 您还未输入数据,请选择 1 或 2 ||\n";
cout << "\t|| ||\n";
cout << "\t =============================================================================\n\n\n\n";
cout << "正在为您跳转到主菜单,请稍等...";
Sleep(2500);
system("CLS");
continue;
}
system("CLS");
ShortTime();
break;
}
case 5: {//退出程序
system("CLS");
Finish_Screen();
exit(0);
break;
default:
cout << "\n\t\t您输入的序号有误,请重新选择,按任意键继续...";
getchar();
getchar();
system("CLS");
break;
}
}
}
return 0;
}
void Start_Screen()
{
system("color 5");//将字体变为深蓝色
cout << "\n\n\n";
cout << "\t\t*************************************************" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t****** ★★欢迎进入★★ ******" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t****** 田径比赛的时间安排系统 ******" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t*************************************************" << endl;
cout << "\n\n\n";
cout << "\t\t正在为您跳转到主菜单,请稍等...";
Sleep(3000);
system("CLS");
}
void Finish_Screen()
{
system("color 9");
cout << "\n\n\n\n\n";
cout << "\t\t*************************************************" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t****** ★★谢谢使用★★ ******" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t****** 田径比赛的时间安排系统 ******" << endl;
cout << "\t\t****** ******" << endl;
cout << "\t\t*************************************************" << endl;
cout << "\n\n\n\n\n";
}
int Menu()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★田径比赛的时间安排系统★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 本届田径比赛的项目种类 ||\n";
cout << "\t|| [1]110米跑步 [2]100米跑步 [3]跳高 ||\n";
cout << "\t|| [4]1000米长跑 [5]800米长跑 [6]110米跨栏 ||\n";
cout << "\t|| [7]100米跨栏 [8]跳远 [9]铅球 ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 功能选项卡 ||\n";
cout << "\t|| ||\n";
cout << "\t|| 【1】--- 从键盘中输入数据 ||\n";
cout << "\t|| 【2】--- 从文件中导入数据 ||\n";
cout << "\t|| 【3】--- 输出邻接矩阵 ||\n";
cout << "\t|| 【4】--- 输出安排的时间 ||\n";
cout << "\t|| 【5】--- 退出程序 ||\n";
cout << "\t|| ||\n";
cout << "\t ==============================================================================\n";
cout << "\t\t\t请输入数字来选择对应的功能:";
int num;
cin >> num;
return num;
}
void DataFromKeyboard()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★你选择的方法是键盘输入★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 本届田径比赛的项目种类 ||\n";
cout << "\t|| [1]110米跑步 [2]100米跑步 [3]跳高 ||\n";
cout << "\t|| [4]1000米长跑 [5]800米长跑 [6]110米跨栏 ||\n";
cout << "\t|| [7]100米跨栏 [8]跳远 [9]铅球 ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 接下来的每一行是该运动员所参加的所有运动项目序号(每人最多参加三项) ||\n";
cout << "\t|| ||\n";
cout << "\t|| 以 0 结束输入 ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
while (c != '0')
{
c = getchar();
if (c == '\n')
{
for (j = 0; j < i - 1; j++)
a[b[j]][b[j + 1]] = a[b[j + 1]][b[j]] = 1;
if (i == 3)
a[b[0]][b[2]] = a[b[2]][b[0]] = 1;
i = 0;
cout << "\t\t\t\t\t";
}
if (c > '0' && c <= '9')
b[i++] = c - 48;
if (c == '0')
break;
}
for (i = 1; i < 10; i++)
{
item = 0;
for (j = 1; j < 10; j++)
{
if (a[i][j])
item++;
}
node[i].item = item;
node[i].number = i;
node[i].color = 0; //0表示初始化无颜色
}
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★数据已成功录入★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t =============================================================================\n";
flag = true;
//着色
Draw(1, 1);
//按颜色排序
qsort(node + 1, 6, sizeof(node[1]), cmp);
cout << "\n\n\n";
cout << "按任意键继续...";
getchar();
getchar();
system("CLS");
}
void DataFromFile()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★你选择的方法是文件导入★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| 请确保文件中的格式为 ||\n";
cout << "\t|| 接下来的每一行是该运动员所参加的所有运动项目序号(每人最多参加三项) ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| 是否继续(y或n) ||\n";
cout << "\t||============================================================================||\n";
char op;//记录用户选择
re://用户输入错误
cout << "\t\t\t\t\t请输入:";
cin >> op;
if (op == 'n' || op == 'N')
{
cout << "即将返回主菜单,请稍等...";
Sleep(3500);
system("CLS");
return;
}
else if (op == 'y' || op == 'Y');
else
{
cout << "\t|| 输入错误重新输入 ||\n";
goto re;
}
char FileName[100]; //文件名
cout << "\t||============================================================================||\n";
cout << "\t\t\t\t 请输入文件名称:";
cin >> FileName;
cout << "\t||============================================================================||\n";
f = fopen(FileName, "r");
c = fgetc(f);
//读取文件并输出
if ((filep = fopen(FileName, "r")) == NULL)
{
cout << "\t\t\t打开文件错误!" << endl;
cout << "\t\t\t即将返回主菜单...";
Sleep(3500);
system("CLS");
return;
}
ch = fgetc(filep);
while (ch != EOF)
{
// putchar(ch);
ch = fgetc(filep);
}
//把文件里的数据录入到矩阵里
i = 0;
while (c != EOF)
{
if (c == '\n')
{
for (j = 0; j < i - 1; j++)
a[b[j]][b[j + 1]] = a[b[j + 1]][b[j]] = 1;
if (i == 3)
a[b[0]][b[2]] = a[b[2]][b[0]] = 1;
i = 0;
}
if (c >= '0' && c <= '9')
b[i++] = c - 48;
c = fgetc(f);
}
for (i = 1; i < 10; i++)
{
item = 0;
for (j = 1; j < 10; j++)
{
if (a[i][j])
item++;
}
node[i].item = item;
node[i].number = i;
node[i].color = 0; //0表示初始化无颜色
}
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★数据已成功录入★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t =============================================================================\n";
flag = true;
//着色
Draw(1, 1);
//按颜色排序
qsort(node + 1, 6, sizeof(node[1]), cmp);
cout << "\n\n\n";
cout << "按任意键继续...";
getchar();
getchar();
system("CLS");
}
int Draw(int k, int color)
{
bool key; //表示是否需要着色
node[k].color = color;
//对于不相连的顶点着一样的颜色
for (j = 1; j < 10; j++)
{
key = true;
if (!a[k][j] && node[j].color == 0)
{
for (i = 1; i < 10; i++)
if (a[j][i] && node[i].color == color)
{
key = false;
break;
}
if (key)
node[j].color = color;
}
}
for (i = 1; i < 10; i++)
if (!node[i].color)
{
Draw(i, color + 1);
break;
}
return OK;
}
int cmp(const void* a, const void* b)
{
struct Node* c = (Node*)a;
struct Node* d = (Node*)b;
return c->color - d->color;
}
void PrintAdjMatrix()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★正在打印邻接矩阵★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
for (int i = 1; i < 10; i++) {
printf("\t\t\t\t ");
for (int j = 0; j < 10; j++) {
printf("%d ", a[i][j]);
}
printf("\n\n");
}
cout << "\t|| ||\n";
cout << "\t =============================================================================\n";
cout << "\n\n\n";
cout << "按任意键继续...";
getchar();
getchar();
system("CLS");
}
//输出比赛时间安排
int ShortTime()
{
cout << endl;
cout << "\t =============================================================================\n";
cout << "\t|| ||\n";
cout << "\t|| ★★★★★★★本届田径比赛的安排如下★★★★★★★ ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
cout << "\t||============================================================================||\n";
cout << "\t|| ||\n";
cout << "\t|| [1]110米跑步 [2]100米跑步 [3]跳高 ||\n";
cout << "\t|| [4]1000米长跑 [5]800米长跑 [6]110米跨栏 ||\n";
cout << "\t|| [7]100米跨栏 [8]跳远 [9]铅球 ||\n";
cout << "\t|| ||\n";
cout << "\t||============================================================================||\n";
int day = 1;
printf("\t\t\t第%d天\t%d", day, node[1].number);
temp = node[1].color;
for (i = 2; i < 10; i++) //输出
{
if (temp != node[i].color)
{
printf("\n");
day++;
printf("\t\t\t第%d天", day);
}
printf("\t%d ", node[i].number);
temp = node[i].color;
}
cout << "\n\t|| ||\n";
cout << "\t =============================================================================\n";
cout << "\n\n\n";
cout << "按任意键继续...";
getchar();
getchar();
system("CLS");
return OK;
}