HDU 5113 Black and White(搜索)

题目:Black and White

题意:给定一个N*M的棋盘,要求用K种颜色对每个格染色,相邻的格的颜色不能相同。并且第i种颜色必须恰好出现c[i]次。求是否存在方案,如果存在,输出任意一种。

先把不可行的情况判断出来。

什么情况不可行呢?肯定就是某种颜色要求出现的次数太多了,怎么样算多呢?考虑一种颜色要尽可能多的染上去,我们需要每间隔一个格染一次。

对于一个N*M的棋盘,最多能染的同一种颜色的个数就是L = N*M/2 + (N*M%2==0?0:1)。

意思其实就是棋盘的一半向上取整。

比如2*4的棋盘最多是4个,而3*3的棋盘最多可以5个。

之前由于SB原因交错代码误以为“构造”的代码能AC,实际是WA。。。直接搜吧。。。

多谢 画船听雨 的提醒~


搜索代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[10][10];
int T, n, m, k;
struct Color{
	int id, cnt;
	bool operator < (const Color &A)const{
		return cnt > A.cnt;
	}
}c[30];
bool ok(int x, int y){
	if(x==n)    return 1;
	if(y==m)    return ok(x+1, 0);
	for(int i=0; i<k; i++){
		if(c[i].cnt){
			if(x && map[x-1][y]==i)    continue;
			if(y && map[x][y-1]==i)    continue;
			c[i].cnt--;
			map[x][y] = i;
			if(ok(x, y+1))    return 1;
			c[i].cnt++;
		}
	}
	return 0;
}
int main(){
	scanf("%d", &T);
	for(int t=1; t<=T; t++){
		scanf("%d %d %d", &n, &m, &k);
		int x = 0;
		for(int i=0; i<k; i++){
			scanf("%d", &c[i].cnt);
			c[i].id = i+1;
			if(c[i].cnt>x)    x = c[i].cnt;
		}
		sort(c, c+k);
		printf("Case #%d:\n", t);
		if((c[0].cnt>(n*m/2+(n*m%2?1:0)))){
			puts("NO");
			continue;
		}
		if(ok(0, 0)){
			puts("YES");
			for(int i=0; i<n; i++){
				for(int j=0; j<m; j++){
					if(j)    putchar(' ');
					printf("%d", c[map[i][j]].id);
				}
				puts("");
			}
		}
		else{
			puts("NO");
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值