Black And White(DFS+剪枝)
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
— 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.
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 .
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.
- #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+剪枝)的更多相关文章
- hdoj 5113 Black And White DFS+剪枝
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
- [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) ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- 《Linux性能及调优指南》----1.1 Linux进程管理
翻译:飞哥 ( http://hi.baidu.com/imlidapeng ) 版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明. 原文名称:<Linux Performance ...
- http://blog.csdn.net/i_bruce/article/details/39555417
http://blog.csdn.net/i_bruce/article/details/39555417
- 【Hadoop】YARN 原理、MR本地&YARN运行模式
1.基本概念 2.YARN.MR交互流程 3.源码解读
- Word文档打不开怎么办
目前一些主流的办公软件给大家日常工作带来了很大便利,比如:Microsoft Office或金山WPS!我们在愉快地使用它们的同时,多少也遇到了一些让人尴尬或头疼的问题,比如:精心制作的文档,突然打不 ...
- Java 实现模板方法(TemplateMethod)模式
类图 /** * 业务流程模板.提供基本框架 * @author stone * */ public abstract class BaseTemplate { public abstract voi ...
- C++ 智能指针详解(转)
C++ 智能指针详解 一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常 ...
- shell脚本通过ping命令来获取平均延时
#!/bin/bash #设置环境变量 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin" exp ...
- 【SpringMVC学习06】SpringMVC中的数据校验
这一篇博文主要总结一下springmvc中对数据的校验.在实际中,通常使用较多是前端的校验,比如页面中js校验,对于安全要求较高的建议在服务端也要进行校验.服务端校验可以是在控制层conroller, ...
- CentOS LVS安装配置
一般2.6.10以上内核版本都已经自带了ipvsadm,故不需要安装. Ipvs 1.25编译 ipvsadm-1.25编译不过 去掉netlink库的依赖:去掉libipvs/Makefile的CF ...
- Struts2 后台获取路径的几种方法
Struts2 后台获取路径的几种方法 package actions.app; import java.io.File; import org.apache.struts2.ServletActio ...