⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 out< "please input the number of the nodes"<<endl cin>>nodesNum cout<<"pl
💻
字号:
//图的着色问题.参见算法基础和笔记
#include<iostream>
#include<cstdlib>      //c++标准程序库里p582,这个文件包括div函数可以求商(quotient)和余数(remainder)  
                       //n.残余, 剩余物, 其他的人, [数]余数v.廉价出售adj.剩余的, 出售剩书的)
                       //该函数返回div_t结构体,这个结构体包括quot和rem
using namespace std;
bool graph[101][101];  //经过编程验证,全局数组默认赋值为0
int x[101];


bool NextValueAbideByRule( int k,int colNum)
{
	int i;
	while(1)
	{
		x[k] = div((x[k]+1),(colNum + 1)).rem;
		if( x[k] == 0)//要回溯,没有一个x(K)符合要求
		{
			return false;
		}
		for( i = 1;i < k; i++)//验证x(k)的合法性
		{
			if( graph[i][k] && x[k] == x[i])
				break;
		}
		if( i == k )//x[k]合法
			return true;
	}//
}

void Output( int n)
{
	int i;
	for(i = 1;i<=n;i++)
		cout<<x[i]<<" ";
	cout<<endl;
}

void Coloring( int n)
{
	//输入为结点的个数n
	int colNum = 3; //颜色的数目
   
	int k = 1;//工作下标
	while( k > 0)
	{
		if( NextValueAbideByRule(k,colNum))
		{
			if( k == n)
		    {
				Output(n);
			}
			else 
				k++;
		}//if
		else 
			k--;
	}//while
}
int main()
{
	int nodeNum;
	cout<<" please input the number of the nodes in the graph"<<endl;
	cin>>nodeNum;
	cout<<"please input the matrix of the graph"<<endl;
	int i,j;
	 for( i = 1; i <= nodeNum ; i++)
	 {
		 cout<<" line "<< i <<endl;
		 for( j = 1; j <= nodeNum ; j++)
			 cin>>graph[i][j];
	 }
	 cout<<"-----------------"
	 Coloring( nodeNum );


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -