您的位置:首页 > 其它

m图着色问题

2009-03-08 18:14 176 查看
#include <iostream>

using namespace std;

class Color
{
friend int mColoring(int,int,int**);
private:
bool OK(int);
void Trace(int);
int n;
int m;
int** a;
int* x;
long sum;
};

bool Color::OK(int k)
{
for(int j = 1; j <= n; j++)
if( (a[k][j] == 1) && (x[j] == x[k]) )	return false;
return true;
}

void Color::Trace(int t)
{
if( t > n )
{
sum ++;
}
else{
for(int i = 1; i<= m; i++)
{
x[t] = i;
if( OK(t))	Trace(t+1);
x[t] = 0;
}
}
}

int mColoring(int n,int m,int** a)
{
Color z;
z.n = n;
z.m = m;
z.a = a;
z.sum = 0;

int* p = new int[n+1];
for(int i = 0; i <= n; i++)
p[i] = 0;

z.x = p;

z.Trace(1);

delete []p;
return z.sum;
}

int main()
{
int nodeNum,colorNum;

cin>>nodeNum>>colorNum;

int** mm = new int*[nodeNum+1];
for(int i = 1; i <= nodeNum; i++)
{
mm[i] = new int[nodeNum+1];
for(int j = 1; j <= nodeNum; j++)
cin>>mm[i][j];
}

cout<<mColoring(nodeNum,colorNum,mm);

for(int i = 1; i <= nodeNum; i++)
delete []mm[i];
delete []mm;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: