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 ...
随机推荐
- log4j教程 1、概述
log4j是一个用Java编写的可靠,快速和灵活的日志框架(API),它在Apache软件许可下发布. Log4j已经被移植到了C,C++,C#,Perl,Python和Ruby等语言中. ...
- 不在JPA 的 persistence.xml 文件里配置Entity class的解决的方法
在Spring 集成 Hibernate 的JPA方式中,须要在persistence配置文件里定义每个实体类.这样很地不方便.2种方法能够解决此问题: 这2种方式都能够实现不用在persist ...
- Solidworks 如何快速完全定义草图
工具-尺寸标注-完全定义草图
- 基于Saltstatck实现页面实时显示tomcat启动日志(17)
一.相关文件 master端: /srv/salt/tomcat/start.sls #tomcat启动服务state.sls,须要自己创建 /srv/salt/tomca ...
- Linux——vi命令的使用
vi的基本操作 a) 进入vi 在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi myfile 不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(command m ...
- vue-router push
//push 方法一 对象 // this.$router.push({path:'HelloWorld2'}); //push 方法二 命名的路由 // this.$router.push({nam ...
- LeetCode题目:Minimum Path Sum
原题地址:https://leetcode.com/problems/minimum-path-sum/ 大意:给出一个二维数组(int类型),求出从左上角到右下角最短的路径. 解决方法:动态规划 c ...
- 【BIEE】由于排序顺序不兼容,集合操作失败
问题描述 使用BIEE数据透视表时,使用了UNION进行数据组合,但是在浏览结果时意外出错了,报错如下截图: 问题分析 原因暂时未知 问题解决 目前使用UNION进行聚合,只需要将UNION修改为UN ...
- jQuery 事件 - delegate() 方法
<html><head><script type="text/javascript" src="/jquery/jquery.js" ...
- JavaScript 获取文件名,后缀名
function getBaseName(str) { var segs = str.split('.'); if(segs.length > 1) segs.pop(); return seg ...