题意:给你一个不超过5X5的矩阵,有k种颜色,每种颜色有c[i]个,问可不可以把格子颜色涂满并且相邻格子的颜色不相同

思路:DFS搜索,不过会超时,留意到一个剪枝就是如果当前没有涂色的格子数目/2比最多还没涂的颜色数量小的话就可以直接返回了,因为肯定会相邻了

#include<bits\stdc++.h>
using namespace std;
int n,m,k;
int mp[6][6];
int color[50];
bool check(int x,int y,int c)
{if (x-1>=1 && mp[x-1][y]==c)return false;if (y-1>=1 && mp[x][y-1]==c)return false;return true;
}
bool dfs(int x,int y)
{for (int i = 1;i<=k;i++)if ((n*m-(m*(x-1)+y-1)+1)/2<color[i])return false;for (int i = 1;i<=k;i++){if (color[i]>0 && check(x,y,i)){color[i]--;mp[x][y]=i;if (x==n && y==m)return true;if (y+1<=m){if (dfs(x,y+1))return true;}else{if (x+1<=n){if (dfs(x+1,1))return true;}}color[i]++;mp[x][y]=-1;}}return false;
}
int main()
{int T,cas=1;scanf("%d",&T);while(T--){memset(mp,-1,sizeof(mp));printf("Case #%d:\n",cas++);scanf("%d%d%d",&n,&m,&k);for (int i = 1;i<=k;i++)scanf("%d",&color[i]);if (dfs(1,1)){puts("YES");for (int i = 1;i<=n;i++){for (int j = 1;j<m;j++)printf("%d ",mp[i][j]);printf("%d\n",mp[i][m]);}}elseputs("NO");}
}

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

HDU 5113 Black And White(搜索+剪枝)相关推荐

  1. DFS+剪枝 hdu 5113 Black And White

    有一个包含从1到K一共K种颜色的N×M棋盘,使得任意两个相邻的区块不能有相同的颜色(如果它们的上.下.左.右任意一边的颜色与自身颜色不同).第i种颜色可以被使用Ci次 思路:因为n,m较小,所以可以想 ...

  2. 深搜、广搜、搜索剪枝

    搜索与回溯讲解 文章目录 深搜 方向向量: DFS代码: 题目讲解: 八皇后问题 字符序列 自然数的拆分 广搜 BFS代码: 题目讲解: 瓷砖 关系网络 bfs与dfs的用途与区别 搜索剪枝 可行性剪 ...

  3. poj 1948(搜索+剪枝)

    解题思路:这道题看到数据量,想到应该搜索+剪枝应该可以过..可是别人的A了,我的却超时了... 我用了一个mark[a][b],表示前两条边长度分别为a和b时,是否已经处理过,如果是的话就直接跳出.. ...

  4. LeetCode 47. 全排列 II(回溯+搜索剪枝)

    文章目录 1. 题目信息 2. 解题 1. 题目信息 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例:输入: [1,1,2] 输出: [[1,1,2],[1,2,1],[2,1,1] ] ...

  5. hdu 1802 Black and white painting(置换群)

    题目链接:hdu 1802 Black and white painting 题意: 有一个n*n的格子,然后用c种颜色去涂,问你有多少种方案. 能旋转,反射的算一种方案. 题解: polya定理的经 ...

  6. 极大极小搜索 α-β剪枝的实现

    极大极小搜索 α-β剪枝的实现 //---------搜索当前搜索状态极大值--------------------------------// //alpha 祖先节点得到的当前最小最大值,用于al ...

  7. 【线段树_区间合并_异或翻转】HDU 3911 Black And White

    HDU 3911 Black And White 题意:有一个只有0和1组成的序列.对这个序列有两个操作:(1)对区间进行翻转.也就是0变1,1变0.  (2)查询区间连续1的序列的最大长度 区间合并 ...

  8. USACO 6.5.3 Betsy's Tour 搜索剪枝

    http://train.usaco.org/usacoprob2?a=BI4LEPNXcEa&S=betsy 题目大意:一个N*N的方阵(N<=7),求从左上角出发到左下角结束经过每个 ...

  9. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 【bfs + 记忆化搜索 + 剪枝】 AC 代码

    ACM 北京区域赛 bfs+剪枝+ms 第一个一遍过的题目,基本没有看题解 记忆搜索当中,注意初始化成一个特殊值:而在访问之后,每个点就会有一个不同于INF(或者 -1等特殊标记)的值 先到先得,适者 ...

最新文章

  1. PHP常用正则表达式汇总 [复制链接]
  2. Educational Codeforces Round 8 D. Magic Numbers 数位DP
  3. sqoop——将mysql数据库的数据表导入到hdfs上
  4. 计算机音乐除了春天爱情和樱花,High4IU《除了春天爱情和樱花》新歌试听
  5. DTRACE简介(2)
  6. 云智能遥控开关设备再物联网领域的应用:智能养殖高效、生态、安全!
  7. java smack 例子_java+smack+openfire即时通讯Im(四)
  8. python3 判断大小端的一种方法
  9. 第6章—渲染web视图—使用Thymeleaf
  10. python plt引用_先引用matplotlib.pyplot再引用tensorflow报错问题
  11. endnotex8使用教程_EndNote X8使用教程
  12. 已解决:Downloading https://ultralytics.com/assets/Arial.ttf to .......ubuntu18.4-YOLOv5报错[01]
  13. Java导出Excel提示文件损坏_导出Gridview到Excel成功但文件已损坏?
  14. 双鱼座男适合学计算机专业,双鱼座男生适合的职业是什么
  15. 【基本办公软件】万彩办公大师教程丨高级计算器的应用
  16. tensor.view().permute()
  17. 数据结构-第一讲 基本概念-学习笔记(MOOC 浙江大学 陈越 何钦铭)
  18. 由DBeaver与PL/SQL引发的数据库吐槽
  19. 食物链顶端的人类被这种“飞蚊”折磨得很痛苦?
  20. 常用电平标准(TTL、CMOS、LVTTL、LVCMOS、ECL、PECL、LVPECL、RS232)

热门文章

  1. 陈艾盐:《春燕》百集访谈节目第六十四集
  2. 手把手教你看懂Chrome火焰图!(调试性能必备)
  3. nc65用友uap开发节点通过按钮调用打开另外一个节点并传数据跳编辑态
  4. Oracle逻辑读详解
  5. 新库上线 | CnOpenData全球摩天大楼数据
  6. 工业互联网-工业企业大数据交换通道-数据通道产品描述
  7. 7-45 宇宙无敌大招呼
  8. 计算机前景 英语作文,计算机与我英语作文范文 The Computer and I
  9. c语言 cuda核函数,CUDA核函数与线程配置
  10. PyTorch转Caffe模型