DFS 着色最少颜色

给一个图,相邻的结点关系已给出,求给图着色最少要多少种颜色。

#include <stdio.h> 
#include <iostream>
using namespace std;

int t;
int count=0;
int arr[20][20];
int color[20]={0};
bool judge(int x){
	   for(int j=1;j<=t;j++){
	       if(arr[x][j]==1 && color[x]==color[j]){
	       	   return false;
		   }
	   }
	return true;
}
int DFS(int index,int num){
	if(index==t+1){
		return 1;
	}
	for(int i=1;i<=num;i++){
		color[index]=i;
		if(judge(index)){
			if(DFS(index+1,num))
			   return 1;
		}
	}
	return 0;
}

void init(){
	count=1;
	for(int i=1;i<t;i++){
		color[i]=0;
	}

}
int main(){
	//freopen("1.txt","r",stdin); //打开文件,测试程序,可以取消这行注释 
    while(cin>>t && t!=0){
        init();
		for(int i=1;i<=t;i++)
    	   for(int j=1;j<=t;j++)
    	      cin>>arr[i][j];
    	for(int i=1;i<=t;i++)
    	    if(DFS(1,i)){
    	    	count=i;
    	    	break;
			}
    	cout<<count<<endl;
	}
	//fclose(stdin);//关闭文件
return 0;
}

测试数据集

2
0 0 
0 0
4
0 1 1 0
1 0 1 1
1 1 0 1
0 1 1 0
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
5
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
0

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值