欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 5113 Black And White DFS+剪枝

程序员文章站 2022-05-20 22:52:03
...

In mathematics, the four color theorem, or the four color map theorem, states that, given any separation of a plane into contiguous regions, producing a figure called a map, no more than four colors are required to color the regions of the map so that no two adjacent regions have the same color. 
— Wikipedia, the free encyclopedia 

In this problem, you have to solve the 4-color problem. Hey, I’m just joking. 

You are asked to solve a similar problem: 

Color an N × M chessboard with K colors numbered from 1 to K such that no two adjacent cells have the same color (two cells are adjacent if they share an edge). The i-th color should be used in exactly c i cells. 

Matt hopes you can tell him a possible coloring.

Input

The first line contains only one integer T (1 ≤ T ≤ 5000), which indicates the number of test cases. 

For each test case, the first line contains three integers: N, M, K (0 < N, M ≤ 5, 0 < K ≤ N × M ). 

The second line contains K integers c i (c i > 0), denoting the number of cells where the i-th color should be used. 

It’s guaranteed that c 1 + c 2 + · · · + c K = N × M . 

Output

For each test case, the first line contains “Case #x:”, where x is the case number (starting from 1). 

In the second line, output “NO” if there is no coloring satisfying the requirements. Otherwise, output “YES” in one line. Each of the following N lines contains M numbers seperated by single whitespace, denoting the color of the cells. 

If there are multiple solutions, output any of them.

Sample Input

4
1 5 2
4 1
3 3 4
1 2 2 4
2 3 3
2 2 2
3 2 3
2 2 2

Sample Output

Case #1:
NO
Case #2:
YES
4 3 4
2 1 2
4 3 4
Case #3:
YES
1 2 3
2 3 1
Case #4:
YES
1 2
2 3
3 1

题意:

一个n*m的数组,然后k种颜色,每种颜色有若干个(你可以把颜色存到一个数组里color【k】),然后相邻的方格颜色不同(共用一条边的方块颜色不同),

输出其中一种方案

只用DFS放颜色 是会超时的,所以我们需要一个剪枝,如果剩下的方格(就是还没有被染色的方格)的数目+1然后除以2,小于任意一种颜色的数目,那就没有必要继续往下搜了,(rem+1)/2<color[i],比如说3*3 的方格,如果你还剩的颜色中一种颜色是6个,那这种方案就不成立(体会一下)。因为9个方格中,颜色一样的最多有5个,

上代码,在代码中体会精髓

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=15;
int ans[maxn][maxn],color[maxn];
int n,m,k,flag,xx,yy;
void init(){//初始化
    flag=0;
	memset(ans,0,sizeof(ans));
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=k;i++)
	scanf("%d",&color[i]);
}
void dfs(int x,int y,int rem){
	if(rem==0) flag=1;
	if(flag==1) return ;
	for(int i=1;i<=k;i++)//很重要的一步剪枝
	if((rem+1)/2<color[i])//如果剩下的空白格子+1/2 小于任何一种颜色的数量,这种方案就不成立
	return ;
	for(int i=1;i<=k;i++)
	{
		if(color[i]&&ans[x-1][y]!=i&&ans[x][y-1]!=i)
		{
			ans[x][y]=i,color[i]--;
			if(y+1>m) xx=x+1,yy=1;
			else xx=x,yy=y+1;
			dfs(xx,yy,rem-1);
			if(flag)return ;
			ans[0][0]=0,color[i]++;
		}
	}
}
void solve(int e){
	printf("Case #%d:\n",e);
	dfs(1,1,n*m);
	if(!flag)
		printf("NO\n");
	else 
	{
		printf("YES\n");
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(j==1) printf("%d",ans[i][j]);
				else printf(" %d",ans[i][j]);
			}
			printf("\n");
		}
	}
} 
int main(){
	int t;
	scanf("%d",&t);
	for(int e=1;e<=t;e++)
	{
		init();
		solve(e);
	}
	return 0;	
};

 

相关标签: 剪枝