HDU 5113 Black And White(搜索+剪枝)
题意:给你一个不超过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
— 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
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
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
Sample Output
HDU 5113 Black And White(搜索+剪枝)相关推荐
- DFS+剪枝 hdu 5113 Black And White
有一个包含从1到K一共K种颜色的N×M棋盘,使得任意两个相邻的区块不能有相同的颜色(如果它们的上.下.左.右任意一边的颜色与自身颜色不同).第i种颜色可以被使用Ci次 思路:因为n,m较小,所以可以想 ...
- 深搜、广搜、搜索剪枝
搜索与回溯讲解 文章目录 深搜 方向向量: DFS代码: 题目讲解: 八皇后问题 字符序列 自然数的拆分 广搜 BFS代码: 题目讲解: 瓷砖 关系网络 bfs与dfs的用途与区别 搜索剪枝 可行性剪 ...
- poj 1948(搜索+剪枝)
解题思路:这道题看到数据量,想到应该搜索+剪枝应该可以过..可是别人的A了,我的却超时了... 我用了一个mark[a][b],表示前两条边长度分别为a和b时,是否已经处理过,如果是的话就直接跳出.. ...
- LeetCode 47. 全排列 II(回溯+搜索剪枝)
文章目录 1. 题目信息 2. 解题 1. 题目信息 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例:输入: [1,1,2] 输出: [[1,1,2],[1,2,1],[2,1,1] ] ...
- hdu 1802 Black and white painting(置换群)
题目链接:hdu 1802 Black and white painting 题意: 有一个n*n的格子,然后用c种颜色去涂,问你有多少种方案. 能旋转,反射的算一种方案. 题解: polya定理的经 ...
- 极大极小搜索 α-β剪枝的实现
极大极小搜索 α-β剪枝的实现 //---------搜索当前搜索状态极大值--------------------------------// //alpha 祖先节点得到的当前最小最大值,用于al ...
- 【线段树_区间合并_异或翻转】HDU 3911 Black And White
HDU 3911 Black And White 题意:有一个只有0和1组成的序列.对这个序列有两个操作:(1)对区间进行翻转.也就是0变1,1变0. (2)查询区间连续1的序列的最大长度 区间合并 ...
- USACO 6.5.3 Betsy's Tour 搜索剪枝
http://train.usaco.org/usacoprob2?a=BI4LEPNXcEa&S=betsy 题目大意:一个N*N的方阵(N<=7),求从左上角出发到左下角结束经过每个 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 【bfs + 记忆化搜索 + 剪枝】 AC 代码
ACM 北京区域赛 bfs+剪枝+ms 第一个一遍过的题目,基本没有看题解 记忆搜索当中,注意初始化成一个特殊值:而在访问之后,每个点就会有一个不同于INF(或者 -1等特殊标记)的值 先到先得,适者 ...
最新文章
- PHP常用正则表达式汇总 [复制链接]
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
- sqoop——将mysql数据库的数据表导入到hdfs上
- 计算机音乐除了春天爱情和樱花,High4IU《除了春天爱情和樱花》新歌试听
- DTRACE简介(2)
- 云智能遥控开关设备再物联网领域的应用:智能养殖高效、生态、安全!
- java smack 例子_java+smack+openfire即时通讯Im(四)
- python3 判断大小端的一种方法
- 第6章—渲染web视图—使用Thymeleaf
- python plt引用_先引用matplotlib.pyplot再引用tensorflow报错问题
- endnotex8使用教程_EndNote X8使用教程
- 已解决:Downloading https://ultralytics.com/assets/Arial.ttf to .......ubuntu18.4-YOLOv5报错[01]
- Java导出Excel提示文件损坏_导出Gridview到Excel成功但文件已损坏?
- 双鱼座男适合学计算机专业,双鱼座男生适合的职业是什么
- 【基本办公软件】万彩办公大师教程丨高级计算器的应用
- tensor.view().permute()
- 数据结构-第一讲 基本概念-学习笔记(MOOC 浙江大学 陈越 何钦铭)
- 由DBeaver与PL/SQL引发的数据库吐槽
- 食物链顶端的人类被这种“飞蚊”折磨得很痛苦?
- 常用电平标准(TTL、CMOS、LVTTL、LVCMOS、ECL、PECL、LVPECL、RS232)