CODE[VS] 1116 四色问题

1116 四色问题

 时间限制: 1 s

 空间限制: 128000 KB

 题目等级 : 黄金 Gold

题解

题目描述 Description

给定N(小于等于8)个点的地图,以及地图上各点的相邻关系,请输出用4种颜色将地图涂色的所有方案数(要求相邻两点不能涂成相同的颜色)

数据中0代表不相邻,1代表相邻

输入描述 Input Description

第一行一个整数n,代表地图上有n个点

接下来n行,每行n个整数,每个整数是0或者1。第i行第j列的值代表了第i个点和第j个点之间是相邻的还是不相邻,相邻就是1,不相邻就是0.

我们保证a[i][j] = a[j][i] (a[i,j] = a[j,i])

输出描述 Output Description

染色的方案数

样例输入 Sample Input

8
0 0 0 1 0 0 1 0 
0 0 0 0 0 1 0 1 
0 0 0 0 0 0 1 0 
1 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 
1 0 1 0 0 0 0 0 
0 1 0 0 0 0 0 0

样例输出 Sample Output

15552

数据范围及提示 Data Size & Hint

n<=8

#include <stdio.h>
#include <iostream>

int map[8][8]={
	0,0,0,1,0,0,1,0,
	0,0,0,0,0,1,0,1,
	0,0,0,0,0,0,1,0,
	1,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,
	0,1,0,0,0,0,0,0,
	1,0,1,0,0,0,0,0,
	0,1,0,0,0,0,0,0
};
int color[8];
void wayOfFourColor(int k,int colorr);
int n;
int count;

int main(){
	scanf("%d",&n);
	
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			scanf("%d",&map[i][j]);
		}
	}
	
	//for(int i=0;i<n;i++){
	//	for(int j=0;j<n;j++){
	//		printf("%d ",map[i][j]);
	//	}
	//	printf("\n");
	//}
	for(int tip=1;tip<=4;tip++){
		wayOfFourColor(0,tip);
	}	
	printf("%d\n\n",count);
	return 0;
} 


void wayOfFourColor(int k,int colorr){
	int sign=0; 
	//printf("k=%d, colorr=%d\n",k,colorr);
	
	for(int linee=0;linee<k;linee++){
		if(map[linee][k] == 1){//存在连接 
			if(color[linee]==colorr){
				sign=1;
			}
		}
	}
	if(sign==1){
		return;
	}else{//check ok
		color[k++]=colorr;//add
		if(k==n){
			count++;//
			return;
		}
		for(int tip=1;tip<=4;tip++){
			//if(tip==colorr){
			//	return;
			//}
			wayOfFourColor(k,tip);
		}	
	}
	color[k-1]=0;	
}

猜你喜欢

转载自blog.csdn.net/u014717398/article/details/86477553