Description

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
  这道题就是搜索,剪枝优化是如果当前未染色的点数为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的更多相关文章

  1. HDU 5113 Black And White 回溯+剪枝

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...

  2. [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) ...

  3. HDU 5113 Black And White ( 2014 北京区预赛 B 、搜索 + 剪枝 )

    题目链接 题意 : 给出 n * m 的网格.要你用 k 种不同的颜色填给出的网格.使得相邻的格子颜色不同.若有解要输出具体的方案 分析 : 看似构造.实则搜索.手构构半天没有什么好想法 直接搜就行了 ...

  4. hdu 5113 Black And White

    http://acm.hdu.edu.cn/showproblem.php?pid=5113 题意:给你n*m的格子,然后在每个格子内涂色,相邻格子不能同色,然后给你每个颜色涂的格子的固定个数,然后可 ...

  5. [sdoi2015]排序(搜索+剪枝优化)

    Description 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1<=i<=N),第i中 ...

  6. poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

    题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一 ...

  7. hdu 5113(2014北京—搜索+剪枝)

    题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...

  8. hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)

    题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...

  9. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. 安卓Intent(显式)

    1.Intent是Android程序中各组件之间交互的重要方式,一般可用于启动活动.启动服务.以及发送广播等场景,这里先对活动进行说明Intent的一些作用. 2.Intent的用法大致可分为,显式I ...

  2. HDU-1114(背包DP)

    Piggy-Bank Problem Description Before ACM can do anything, a budget must be prepared and the necessa ...

  3. C#DbHelperOra,Oracle数据库帮助类 (转载)

    主要功能如下数据访问抽象基础类 主要是访问Oracle数据库主要实现如下功能 .数据访问基础类(基于Oracle),主要是用来访问Oracle数据库的. .得到最大值:是否存在:是否存在(基于Orac ...

  4. proxy.ini文件调用

    self.CONFIG_FILENAME = os.path.splitext(os.path.abspath(__file__))[0]+'.ini' 改为: self.CONFIG_FILENAM ...

  5. maven中tomcat7-maven-plugin插件的使用

    1.(挺清晰,但是我在项目上尝试没有成功) http://blog.csdn.net/yhhazr/article/details/7866501 2.(算是有一些详细的运行命令吧,例如自动打包命令或 ...

  6. WPF RadioButton & CheckBox Style

    <Style TargetType="CheckBox"> <Setter Property="Template"> <Sette ...

  7. 1.document.write(""); 输出语句

    1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...

  8. ubuntu npm 私有库搭建 (npmjs.org 官方版本)

    目标 npm.xxx.com 安装和推送nodejs包 npmui.xxx.com 管理已经推送的nodejs包   安装 couchdb   https://launchpad.net/~couch ...

  9. 最常见的HTTP错误

    1. HTTP 500错误(内部服务器错误)对对HTTP 500错误的定义已经充分证明了这是一个最常见的HTTP错误. 一般来说,HTTP 500 错误就是web服务器发生内部错误时返回的信息. 例如 ...

  10. hw-text1

    Text 1 测试题 python是什么类型的语言? 解释型语言,是脚本语言 百娘(脚本语言是为了缩短传统的编写-编译-链接-运行(edit-compile-link-run)过程而创建的计算机编程语 ...