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
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <set>
  11. #include <vector>
  12. using namespace std;
  13. #define LL long long
  14. #define PI acos(-1.0)
  15. #define lowbit(x) (x&(-x))
  16. #define INF 0x7f7f7f7f // 21 E
  17. #define MEM 0x7f // memset 都变为 INF
  18. #define MOD 4999 // 模
  19. #define eps 1e-9 // 精度
  20. #define MX 10 // 数据范围
  21.  
  22. int read() { //输入外挂
  23. int res = , flag = ;
  24. char ch;
  25. if((ch = getchar()) == '-') flag = ;
  26. else if(ch >= '' && ch <= '') res = ch - '';
  27. while((ch = getchar()) >= '' && ch <= '') res = res * + (ch - '');
  28. return flag ? -res : res;
  29. }
  30. // code... ...
  31. int n,m,k;
  32. int ok;
  33. int color[MX*MX];
  34. int num[MX][MX];
  35.  
  36. void dfs(int x,int y)
  37. {
  38. if (x>n) ok=;
  39. for (int i=;i<=k;i++)
  40. {
  41. int remain = n*m-((x-)*m+y-)+;
  42. if (color[i]>remain/) return;
  43. }
  44. for (int i=;i<=k;i++)
  45. {
  46. if (color[i]>&&num[x-][y]!=i&&num[x][y-]!=i)
  47. {
  48. num[x][y]=i;
  49. color[i]--;
  50. if (y==m) dfs(x+,);
  51. else dfs(x,y+);
  52. color[i]++;
  53. if (ok) return;
  54. }
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. int T=read();
  61. for (int cnt=;cnt<=T;cnt++)
  62. {
  63. n=read();m=read();k=read();
  64. for (int i=;i<=k;i++)
  65. color[i]=read();
  66. ok = ;
  67. memset(num,,sizeof(num));
  68. dfs(,);
  69. printf("Case #%d:\n",cnt);
  70. if (ok)
  71. {
  72. printf("YES\n");
  73. for (int i=;i<=n;i++)
  74. {
  75. for (int j=;j<m;j++)
  76. printf("%d ",num[i][j]);
  77. printf("%d\n",num[i][m]);
  78. }
  79. }
  80. else printf("NO\n");
  81. }
  82. return ;
  83. }
 

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. 《Linux性能及调优指南》----1.1 Linux进程管理

    翻译:飞哥 ( http://hi.baidu.com/imlidapeng ) 版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明. 原文名称:<Linux Performance ...

  2. http://blog.csdn.net/i_bruce/article/details/39555417

    http://blog.csdn.net/i_bruce/article/details/39555417

  3. 【Hadoop】YARN 原理、MR本地&YARN运行模式

    1.基本概念 2.YARN.MR交互流程 3.源码解读

  4. Word文档打不开怎么办

    目前一些主流的办公软件给大家日常工作带来了很大便利,比如:Microsoft Office或金山WPS!我们在愉快地使用它们的同时,多少也遇到了一些让人尴尬或头疼的问题,比如:精心制作的文档,突然打不 ...

  5. Java 实现模板方法(TemplateMethod)模式

    类图 /** * 业务流程模板.提供基本框架 * @author stone * */ public abstract class BaseTemplate { public abstract voi ...

  6. C++ 智能指针详解(转)

    C++ 智能指针详解   一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常 ...

  7. shell脚本通过ping命令来获取平均延时

    #!/bin/bash #设置环境变量 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin" exp ...

  8. 【SpringMVC学习06】SpringMVC中的数据校验

    这一篇博文主要总结一下springmvc中对数据的校验.在实际中,通常使用较多是前端的校验,比如页面中js校验,对于安全要求较高的建议在服务端也要进行校验.服务端校验可以是在控制层conroller, ...

  9. CentOS LVS安装配置

    一般2.6.10以上内核版本都已经自带了ipvsadm,故不需要安装. Ipvs 1.25编译 ipvsadm-1.25编译不过 去掉netlink库的依赖:去掉libipvs/Makefile的CF ...

  10. Struts2 后台获取路径的几种方法

    Struts2 后台获取路径的几种方法 package actions.app; import java.io.File; import org.apache.struts2.ServletActio ...