图的m着色问题

#include<iostream>
#include<iomanip>
using namespace std;
const int n = 5;
const int m = 4;
int sum = 0;
const int Graph[n][n] = {
	{0,1,1,1,0},
	{1,0,1,1,1},
	{1,1,0,1,0},
	{1,1,1,0,1},
	{0,1,0,1,0}
};
int Color[n] = { 0 };//0是未着色状态
bool check(int c);//检查c点与已着色的邻接点颜色是否不同
void Print(int c,int Count=1);//给点c上色,Count记录有多少个点已经着色
int main()
{
	Print(0);//初始状态只选取一个点,避免重复
	return 0;
}

bool check(int c)
{
	for (int i = 0; i < n; i++)
	{
		if ((Graph[c][i] == 1) && (Color[i] != 0) && (Color[i] == Color[c]))
			//Graph[c][i]==1表示i是c的邻接点,Color[i]!=0表示i已经上色,Color[i]==Color[c]表示i和c上色的颜色相同
			return false;
	}
	return true;
}

void Print(int c,int Count)
{
	for (int k = 1; k <= m; k++)
	{
		Color[c] = k;
		if (check(c))//着色成功
		{
			if (Count == n)//所有点都已经着色
			{
				cout<<"No." << ++sum <<": ";
				for (int i = 0; i < n; i++)
					cout << setw(3) << Color[i];
				cout << endl;
			}
				
			else
			{
				for (int i = c+1; i < n; i++)
				{
					if ((Graph[c][i] == 1) && (Color[i] == 0))
						//Graph[c][i]==1表示i是c的邻接点,Color[i]==0表示i没有着过色
						Print(i, Count + 1);
				}
			}
		}
	}
	Color[c] = 0;
	return;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值