题意:告诉格子规格,颜色个数,以及每个颜色能涂得格子数目,问是否能够实现相邻两个格子的颜色数目不相同。

分析:因为数据很小,格子最多是5 * 5大小的,因此可以dfs。TLE了一次之后开始剪枝,31ms过。剪枝看代码。

 #include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define pfi(n) printf("%d\n", n)
#define MAXN 26
int n, m, k, tot;
int c[MAXN];
int mpp[][], re[][];
int tcc[MAXN];
void put(int mp[][])
{
repu(i, , n)
{
repu(j, , m)
if(j == )
printf("%d", mp[i][j]);
else printf(" %d", mp[i][j]);
puts("");
}
}
bool Judge(int tc[], int cur)///剪枝,剩下没染的颜色中的数目如果超过一半就break
{
repu(i, , k) if(c[i] - tc[i] > (tot - cur + ) / ) return true;
return false;
}
int debug(int cur,int mp[][])
{
cout<<cur<<endl;
put(mp);
}
bool dfs(int cur, int tc[], int mp[][])
{
///当前已经染得格子数目,当前染得每个颜色剩下的数目,当前的已经订好的方案
///debug(cur,mp);
if(cur >= n * m) return true;
int x = cur / m;
int y = cur - x * m;
if(Judge(tc, cur)) return false;
repu(i, , k)
{
if(tc[i] < c[i])
{
if(x - >= && mp[x - ][y] == i + )
continue;
if(y - >= && mp[x][y - ] == i + )
continue;
tc[i]++;
mp[x][y] = i + ;
if(dfs(cur + , tc, mp))
return true;
tc[i]--;
}
}
return false;
}
int main()
{
int T;
sfi(T);
repu(kase, , T + )
{
sfi(n), sfi(m), sfi(k);
tot = n * m;
repu(i, , k) sfi(c[i]);
_cle(tcc, );
_cle(mpp, );
printf("Case #%d:\n", kase);
if(dfs(, tcc, mpp))
{
printf("YES\n");
put(mpp);
}
else
printf("NO\n");
}
return ;
}

HDU 5113 dfs剪枝的更多相关文章

  1. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  2. hdu 4109 dfs+剪枝优化

    求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己添加的)出发,0到1~n个节点之间的距离为1.mt[i]表示从0点到第i个节点眼下所得的最长路径 #include<iost ...

  3. Tempter of the Bone HDU 1010(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. hdu 1518 dfs+剪枝

    题目大意:几根棒子能否组成一个正方形 Sample Input3           //测试组数4 1 1 1 1   //棒子数目以及每根棒子的长度5 10 20 30 40 508 1 7 2 ...

  5. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  8. hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...

  9. HDU 5113

    HDU 5113类似四色定理的什么东西,大体就是dfs了,不过有两个坑点,这个题的逼格瞬间就上去了1.剪枝很神奇,任何一种颜色都不能超过剩下总格子数的一半,想想确实显然但是比赛的时候没有想到:2.测评 ...

随机推荐

  1. 解决Strokeit在win8下的图标问题和开机启动问题

    Strokeit目前和Windows 8有一点不兼容,就是运行之后,任务栏会有它的图标,看着很不爽,用兼容模式运行可解决这个问题,但是这样一来就不能开机自动运行了,本文主要解决这个问题.  (参考资料 ...

  2. List接口、Set接口、Map接口的方法

    一.Collection接口中的方法介绍 int size();返回此Collection中的元素数 boolean isEmpty(); 判断是否为空 boolean containsAll(Col ...

  3. 获取checkbox后面的文本内容

    http://alygle.blog.51cto.com/1922399/669040 <head> <meta http-equiv="Content-Type" ...

  4. WEB安全性测试

    SQL注入   所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序,将(恶意)的SQL命令 ...

  5. oracle date change

    select to_char(sysdate,'yyyy') from dual;-----2016select to_number(to_char(sysdate,'MM'))||'月' from ...

  6. Angular js 之动态传数据到下一个页面和动态通过ng-click进入不同的页面

    +关于Angular js中一些千篇一律的后台获取数据 首先在services.js里面把服务写好 然后在controller里面把数据给打印出来 (首先需要把数据注入) +关于Angular js中 ...

  7. RP4412开发板在Android系统编译生成ramdisk-uboot.img

    荣品RP4412开发板在android系统编译的时候,怎么生成ramdisk-uboot.img生成流程分析: mkimage -A arm -O linux -T ramdisk -C none - ...

  8. 汽车驱动之家 http://bmw360.cn

    汽车驱动之家 http://bmw360.cn

  9. 对copy、mutableCopy理解

    Objective - C 中有很多在日常项目中经常用到的常用代码,在这里着重的讲一下关于copy 和 mutableCopy 的区别以及相关用法. Objective - C 中可变对象和不可对象经 ...

  10. Sql server 2008 中varbinary查询

    sqlserver2008中遇到一个坑爹的问题,使用以下语句添加的数据 insert into testtable ( username, password, productcode ) select ...