无向图染色问题
#include<iostream>
using namespace std;
int map[100][100]={0}; //邻接矩阵
int v=5; //顶点数
int color_s=4; //颜色数
int color[100]={0}; //颜色标记数组
int ok(int depth,int c){ //检查当前点是否可以着色C
for(int i=1;i<=v;i++)
if(map[depth][i]==1||map[i][depth]==1){
if(color[i]==0) //邻居没有染色
continue;
if(c==color[i]) //相同颜色不能染
return 0;
}
return 1;
}
void dfs(int depth){
if(depth>v){ //一种染色方式
for(int i=1;i<=v;i++)
cout<<color[i]<<' ';
cout<<endl;
}
else{
for(int i=1;i<=color_s;i++){//枚举每种颜色
if(ok(depth,i)==1){
color[depth]=i;
dfs(depth+1);//递归
color[depth]=0;//回溯
}
}
}
}
int main(){
map[1][2]=1;
map[1][3]=1;
map[1][4]=1;
map[2][3]=1;
map[2][4]=1;
map[2][5]=1;
map[3][4]=1;
map[4][5]=1;
dfs(1);
return 0;
}