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. Nginx user_agent、if指令及全局变量

    Nginx user_agent.if指令及全局变量 1.User_agent User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CP ...

  2. Node.js node主文件找不到时报出的Error:Cannot find module异常

    如果执行>Node xx.js时,xx,js找不到的话,报出的错误是Error:Cannot find module 'c:/test/xx,js' 主文件名打错或者是点号打成逗号都会出这样的错 ...

  3. C++_友元函数(转)

    1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 具体来说:为了使其他类的成员函数直接访问该类的私有变量 即:允许外面的类或函数去访问类的私有变量和保护变量,从而使两个类共享同 ...

  4. 仿IOS中下拉刷新的“雨滴”效果

    在IOS中,有非常赞的"水滴"下拉效果.非常久之前也想在Android上实现,可是苦于能力有限,一直未能付诸行动.这几天趁着空隙时间.写了一版初步实现,基本达到了"水滴& ...

  5. MySQL性能优化的最佳20+条经验(转)

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序 员需要去关注的事情.当我们去设计数据库表结构,对操作数 ...

  6. 微信小程序 - 豆瓣同城

    代码地址如下:http://www.demodashi.com/demo/12121.html 一.准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com/ ...

  7. JDBC技术总结(一)

    1. JDBC简介 SUN公司为了简化.统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC,JDBC不能直接操作数据库,JDBC通过接口加载数据库的驱动,然后操作数据库.JDBC: ...

  8. The Application does not have a valid signature

    真机运行程序,报错(The application does not have a valid signature,如图 环境:Xcode7.3,使用cocoapods管理第三方库 如果确认证书没有问 ...

  9. .Net、C# 汉字转拼音,简体繁体转换方法

    Visual Studio International Pack 包含一组类库,该类库扩展了.NET Framework对全球化软件开发的支持.使用该类库提供的类,.NET 开发人员可以更方便的创建支 ...

  10. matlab-非线性方程求根函数及函数曲线绘制

    Matlab中提供了很多求解非线性方程(y=f(x))的函数,刚開始使用,真的很困惑.全部.这里依据matlab的help文档对这些函数做一些小小的总结 fsolve函数 用来求解非线性方程组:F(x ...