HDU 5113--Black And White(搜索+剪枝)
— 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 ci cells.
Matt hopes you can tell him a possible coloring.
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 ci (ci > 0), denoting the number of cells where the i-th color should be used.
It’s guaranteed that c1 + c2 + · · · + cK = N × M .
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.
题意:有一个N*M的方格板,现在要在上面的每个方格上涂颜色,有K种颜色,每种颜色分别涂c[1]次、c[2]次……c[K]次,c[1]+c[2]+……+c[K]=N*M
要求每个方格的颜色与其上下左右均不同,如果可以输出YES,并且输出其中的一种涂法,如果不行,输出NO;
思路:暴力搜索,但是这样会超时,可以在搜索中加入剪枝:对于剩余的方格数res,以及当前剩余的颜色可涂数必须满足(res+1)/2>=c[i]
否则在当前情况下继续向下搜得不到正确涂法;
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int N,K,M;
int c[];
int mp[][]; int check(int x,int y,int k)
{
int f=;
if(mp[x-][y]==k) f=;
if(mp[x][y-]==k) f=;
return f;
} int cal(int x,int y)
{
if(x>N) return ;
int res=(N-x)*M+M-y+; ///剩余方格数+1 ;
for(int i=;i<=K;i++) if(res/<c[i]) return ; ///剪枝,某种颜色剩余方格数>(剩余方格数+1)/2 肯定不对;
for(int i=;i<=K;i++)
{
int f=;
if(c[i]&&check(x,y,i)){
mp[x][y]=i; c[i]--;
if(y==M) f=cal(x+,);
else f=cal(x,y+);
c[i]++;
}
if(f) return ;
}
return ;
} int main()
{
int T,Case=;
cin>>T;
while(T--)
{
scanf("%d%d%d",&N,&M,&K);
for(int i=;i<=K;i++) scanf("%d",&c[i]);
printf("Case #%d:\n",Case++); if(!cal(,)) { puts("NO"); continue; }
puts("YES");
for(int i=;i<=N;i++)
for(int j=;j<=M;j++)
printf("%d%c",mp[i][j],(j==M)?'\n':' ');
}
return ;
}
HDU 5113--Black And White(搜索+剪枝)的更多相关文章
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- [HDU 5113] Black And White (dfs+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题目大意:给你N*M的棋盘,K种颜色,每种颜色有c[i]个(sigma(c[i]) = N*M) ...
- hdu 5113(2014北京—搜索+剪枝)
题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...
- 搜索(剪枝优化):HDU 5113 Black And White
Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...
- HDU 5113 Black And White ( 2014 北京区预赛 B 、搜索 + 剪枝 )
题目链接 题意 : 给出 n * m 的网格.要你用 k 种不同的颜色填给出的网格.使得相邻的格子颜色不同.若有解要输出具体的方案 分析 : 看似构造.实则搜索.手构构半天没有什么好想法 直接搜就行了 ...
- 【HDU 6171】Admiral(搜索+剪枝)
多校10 1001 HDU 6171 Admiral 题意 目标状态是第i行有i+1个i数字(i=0-5)共6行.给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个.求20步以内到目标状态 ...
- HDU 1045 Fire Net(搜索剪枝)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045 ...
- hdu 5113 Black And White
http://acm.hdu.edu.cn/showproblem.php?pid=5113 题意:给你n*m的格子,然后在每个格子内涂色,相邻格子不能同色,然后给你每个颜色涂的格子的固定个数,然后可 ...
- hdoj 5113 Black And White DFS+剪枝
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
随机推荐
- Linux 上pcntl安装步骤
一. 下载对应的PHP源码包 wget http://cn2.php.net/get/php-5.5.20.tar.gz/from/this/mirror 二. 解压下载的源码文件 tar -zxvf ...
- MySQL经典练习题
表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id ...
- linux 查找java程序、杀死、重启
查看java进程 ps -ef|grep java杀死进程 kill -9 4834 (进程号)启动 java -jar xxx.jar & (后台会一直运行)
- 搭建中小规模集群之rsync数据同步备份
NFS重要问题 1.有关NFS客户端普通用户写NFS的问题. 1)为什么要普通用户写NFS. 2)exports加all_squash. Rsync介绍 什么是Rsync? Rsync是一款开源的.快 ...
- FortiGate防火墙对数据包处理流程
1.流程图 2.防火墙对数据包处理过程的各步骤如下: 1)Interface(网卡接口) 网卡接口驱动负责接数收据包,并转交给下一过程. 2)DoS Sensor(DoS防御,默认关闭) 负责过滤SY ...
- [leetcode]91. Decode Ways解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- Jstack定位CPU使用最多的线程及代码
jstack可以定位到线程堆栈,根据堆栈信息我们可以定位到具体代码,所以它在JVM性能调优中使用得非常多.下面我们来一个实例找出某个Java进程中最耗费CPU的Java线程并定位堆栈信息,用到的命令有 ...
- javaweb开发.eclipse使用小常识
一.javabean快速生成 1.写好属性,如 package me.lst.domain; import java.io.Serializable;import java.sql.Date; /** ...
- pytbull:入侵检测/预防系统测试框架 (转)
pytbull:入侵检测/预防系统测试框架 或许当你安装了 IDS/IPS(入侵检测/预防系统)之后就感觉系统安全无忧了,但如何确信?答案是测试.pytbull 是使用 Python 开发而成的 ID ...
- nc6 用业务插件注册来跑按钮事件
在实际开发中,有些需求是要求系统单据,编辑或者触发其他按钮来回写其他模块单据 这时候就能用业务插件方式来触发其他模块的按钮事件,而不用去模块找对应的按钮编辑事件类 package hz.bs.hzct ...