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. instanceof 含义

    看到一个问题: 把一个字面量对象,变成某个类的实例 function Type() {} var a = {}; ______________ // a instanceof Type === tru ...

  2. 关于 ORA - 01861 文字与格式字符串不匹配问题(oracle存储过程)

    一般问题(TO_DATE 和 TO_CHAR 两种格式互换)比如: 只要转化下格式就OK ,这里就不详细解释这两种格式的用法了! 今天把之前做好的模块拿到当地实习,不管怎么测 ,连续测试了好几个存储过 ...

  3. java.sql.Date to java.util.Date

    发这篇博文的题目可能无法直接表示内容,但是确实是java.sql.Date和java.util.Date. 今天在使用'net.sf.json.JSONObject'封装json数据的时候,碰到很奇怪 ...

  4. 在mipsel-linux平台上的编译应用SQLite-3.5.9

    sqlite 第一个Alpha版本诞生于2000年5月,是实现了SQL 92标准的一个大子集的嵌入式数据库,其以在一个库中组合了数据库引擎和接口,能将所有数据存储于单个文件中.官方测试表明sqlite ...

  5. Linux使用fdisk进行磁盘管理

            Fdisk分区工具1. Overview*Fdisk是IBM的老牌分区工具,支持绝大多数操作系统,几乎所有的Linux操作系统都默认装有fdisk:包括在Linux Rescue模式下 ...

  6. CSS Text文本格式

    Text Color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如"#FF0000" 一个RGB值 - "RGB(255,0,0)& ...

  7. UVA 11825 Hackers’ Crackdown(集合动态规划 子集枚举)

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...

  8. XML&DTD&XML Schema学习

    XML(eXtensible Markup Language)可扩展的标记语言.xml在web service编程中尤为重要.在网络传输中可以作为传输数据的载体.xml作为元语言,它可以用来标记数据. ...

  9. 『重构--改善既有代码的设计』读书笔记----Replace Array with Object

    如果你有一个数组,其中的元素各自代表不同东西,比如你有一个 QList<QString> strList; 其中strList[0]代表选手姓名,strList[1]代表选手家庭住址,很显 ...

  10. CentOS6.3安装VBoxAdditions

    yum update kernel yum install kernel-devel gcc gcc-c++