Black And White

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 3937    Accepted Submission(s): 1082
Special Judge

Problem 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 ci 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 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 .

 
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
 
 
//题意:第一行一个 T ,然后3个整数 n,m,k 代表有个 n 行 m 列的矩阵,k 代表有k种颜色,然后是 k 种颜色分别有多少个
要用这些颜色涂满矩阵,相邻颜色不能相同,问能否填出
 
DFS+剪枝,剪枝就是,如果有一种颜色比剩下的格子+1的一半还多的话,就 return
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define LL long long
#define PI acos(-1.0)
#define lowbit(x) (x&(-x))
#define INF 0x7f7f7f7f // 21 E
#define MEM 0x7f // memset 都变为 INF
#define MOD 4999 // 模
#define eps 1e-9 // 精度
#define MX 10 // 数据范围 int read() { //输入外挂
int res = , flag = ;
char ch;
if((ch = getchar()) == '-') flag = ;
else if(ch >= '' && ch <= '') res = ch - '';
while((ch = getchar()) >= '' && ch <= '') res = res * + (ch - '');
return flag ? -res : res;
}
// code... ...
int n,m,k;
int ok;
int color[MX*MX];
int num[MX][MX]; void dfs(int x,int y)
{
if (x>n) ok=;
for (int i=;i<=k;i++)
{
int remain = n*m-((x-)*m+y-)+;
if (color[i]>remain/) return;
}
for (int i=;i<=k;i++)
{
if (color[i]>&&num[x-][y]!=i&&num[x][y-]!=i)
{
num[x][y]=i;
color[i]--;
if (y==m) dfs(x+,);
else dfs(x,y+);
color[i]++;
if (ok) return;
}
}
} int main()
{
int T=read();
for (int cnt=;cnt<=T;cnt++)
{
n=read();m=read();k=read();
for (int i=;i<=k;i++)
color[i]=read();
ok = ;
memset(num,,sizeof(num));
dfs(,);
printf("Case #%d:\n",cnt);
if (ok)
{
printf("YES\n");
for (int i=;i<=n;i++)
{
for (int j=;j<m;j++)
printf("%d ",num[i][j]);
printf("%d\n",num[i][m]);
}
}
else printf("NO\n");
}
return ;
}
 

Black And White(DFS+剪枝)的更多相关文章

  1. hdoj 5113 Black And White DFS+剪枝

    Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...

  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. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  5. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  6. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  7. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  8. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. django 配置上传图片和文件

    在django中经常遇到要上传文件的需求,这里记录下如何配置用户上传的文件保存 首先在setting中添加 TEMPLATES = [ { 'BACKEND': 'django.template.ba ...

  2. python virtualenv virtualenvwrapper

    python中的virtualenv模块能够将项目环境分隔开,而不是使用全局的环境,非常实用. 首先pip install virtualenv 如何创建一个环境virtualenv testvir ...

  3. .Net程序测试使用阿里云OCS开放缓存服务

     首先需要有一个阿里的OCS实例和ECS云服务器 请确认这两个是在同一个可用区的,这个很重要! 这两个可以在阿里云官网申请得到 拿到OCS之后 进入OCS控制台,点击下面的客户端下载选择.Net客 ...

  4. Python 自用代码(scrapy多级页面(三级页面)爬虫)

    2017-03-28 入职接到的第一个小任务,scrapy多级页面爬虫,从来没写过爬虫,也没学过scrapy,甚至连xpath都没用过,最后用了将近一周才搞定.肯定有很多low爆的地方,希望大家可以给 ...

  5. ECSHOP搜索框文字点击消失

    <input name="keywords" type="text" id="keyword" value="黄山金银币&q ...

  6. PS PNG导出的时候是否交错有什么影响

    已解决 导出png格式交错什么意思 我百度的答案一律说png支持交错,可是交错两个字的意思是什么啊,那位专家请指教.谢谢啦!! 问题补充: 我用photoshop或者coreldraw制作按钮时候不是 ...

  7. react-native 项目实战 -- 新闻客户端(2) -- 完善TabBar

    1.创建 drawable-xxhdpi 文件夹,保存 TabBar 的 icon图标 android  --  app  --  src  --  main  --  res  --  drawab ...

  8. Oracle ODBC无Oracle连接驱动

    .下载odbc驱动 需要下载两个东西 instantclient.zip instantclient.zip 下载地址:http:.html 解压放到同一个目录(无冲突) .将oracle数据库所在电 ...

  9. mongo: 改

    语法:db.CollectionName.upadte(查询表达式,新值,选项); 查询表达式:定位哪些列是要被修改的列(即使查询表达式能命中多行,默认也只改一行,如果想改多行,可以用multi选项, ...

  10. 【Lucene】Apache Lucene全文检索引擎架构之入门实战1

    Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供.Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻.在Java开发环境里Lucene是一个成熟的 ...