搜索(剪枝优化):HDU 5113 Black And White
Description
― 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
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
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
这道题就是搜索,剪枝优化是如果当前未染色的点数为x,(x+1)/2<max(col[i]),就可以return了。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
int id[N][N],L[N*N],U[N*N],c[N*N];
int T,cas,n,m,k,map[N*N],p[N*N];
bool DFS(int x){
if(x==n*m+)return true;
for(int i=;i<=k;i++)
if((n*m+-x)/<c[i])return false;
for(int i=;i<=k;i++){
if(c[i]==)continue;
if(map[L[x]]!=i&&map[U[x]]!=i){
c[i]-=;map[x]=i;
if(DFS(x+))return true;
c[i]+=;map[x]=;
}
}
return false;
}
int main(){
scanf("%d",&T);
while(T--){int idx=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++)
scanf("%d",&c[i]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
id[i][j]=++idx;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
L[id[i][j]]=id[i][j-];
U[id[i][j]]=id[i-][j];
}
printf("Case #%d:\n",++cas);
if(DFS()){
puts("YES");
for(int i=;i<=n;i++){
for(int j=;j<m;j++)
printf("%d ",map[(i-)*m+j]);
printf("%d\n",map[i*m]);
}
}
else puts("NO");
}
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 Black And White ( 2014 北京区预赛 B 、搜索 + 剪枝 )
题目链接 题意 : 给出 n * m 的网格.要你用 k 种不同的颜色填给出的网格.使得相邻的格子颜色不同.若有解要输出具体的方案 分析 : 看似构造.实则搜索.手构构半天没有什么好想法 直接搜就行了 ...
- hdu 5113 Black And White
http://acm.hdu.edu.cn/showproblem.php?pid=5113 题意:给你n*m的格子,然后在每个格子内涂色,相邻格子不能同色,然后给你每个颜色涂的格子的固定个数,然后可 ...
- [sdoi2015]排序(搜索+剪枝优化)
Description 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1<=i<=N),第i中 ...
- poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)
题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一 ...
- hdu 5113(2014北京—搜索+剪枝)
题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...
- hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)
题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- 使用linq获得当前文件夹下的下一级满足条件的文件夹
使用linq获得当前文件夹下的下一级满足条件的文件夹. SPFolderCollection subAlbums = Folder.SubFolders; ...
- Entity Framework性能优化
AsNonUnicode 执行如下语句,并用SqlProfiler监控其SQL: var list = WMFactory.ReChargeMobile.Queryable().Where(w =&g ...
- 学习java随笔第七篇:java的类与对象
类 同一个包(同一个目录),类的创建与调用 class Man{ String name; void GetMyName() { System.out.println(name); } } publi ...
- html复选框多行排列布局
前言:写这篇文章,主要是在于总结一下自己的心得体会... 公司的产品需要实现操作权限配置,我们实现的方式是,左边是“产品”=>“模块”树,右边是由“菜单”和“按钮”复选框按钮.如下图:
- Jenkins integration for AngularJS code coverage
Install Jenkins plugins 'Cobertura' and 'HTML Publisher' 1. add Post-build Actions "Publish HTM ...
- 【html】【1】html的简单结构
1>我们为什么能打开html网页 计算机本身有个配置文件后缀打开程序的默认,好比:.txt =>记事本打开 html=>浏览器打开 文件一切的鼻祖都起源于.txt文件,你只要修改 ...
- AdaBoost原理,算法实现
前言: 当做重要决定时,大家可能综合考虑多个专家而不是一个人的意见.机器学习处理问题也是如此,这就是元算法背后的思路.元算法是对其他算法进行组合的一种方式,前几天看了一个称作adaboost方法的介绍 ...
- WPF 进度条
//Create a Delegate that matches the Signature of the ProgressBar's SetValue method private delegate ...
- 【随记】关于List集合用Linq GroupBy分组过后的遍历小记
List<LeaderKaoQin> lstLeader = new List<LeaderKaoQin>();//一个List集合IGrouping<string, L ...
- JavaScript学习心得(八)
Cookie是Netscape发明的技术,是动态网站必不可少的部分,用于浏览器请求Web页面的超文本传输协议是一种无状态的协议. 两种方法维护状态:使用会话(session)(使用服务器技术实现,数据 ...