无向图着色问题

无向图染色问题

#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;
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值