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

分析:因为数据很小,格子最多是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. 51nod 1622 集合对[算法马拉松19 C]

    题目链接:https://www.51nod.com/contest/problem.html#!problemId=1622 第一次参加算法马拉松,我就是去看大神们疯狂秒题,然后感受绝望的orz.. ...

  2. jQuery验证插件 Validate详解

    1.引入必要的文件,以及语言中文提示包 2.使用规则如下: 序号 规则 描述 1 required:true 必须输入的字段. 2 remote:"check.php" 使用 aj ...

  3. ubuntu16.04 orbslam ./build.sh 出错eigen

    错误如下: /home/a/ORB_SLAM2/src/Optimizer.cc:1244:1: required from here/usr/include/eigen3/Eigen/src/Cor ...

  4. [整]磁盘 I/O 性能监控指标和调优方法

    在介绍磁盘 I/O 监控命令前,我们需要了解磁盘 I/O 性能监控的指标,以及每个指标的所揭示的磁盘某方面的性能. 磁盘 I/O 性能监控的指标主要包括: 指标 1:每秒 I/O 数(IOPS 或 t ...

  5. 学习solr

    1.下载地址 http://apache.opencas.org/lucene/solr/

  6. pct xcode7

    1.) 打开你的Xcode工程. 在Supporting Files目录下,选择 File > New > File > iOS > Other > PCH File 然 ...

  7. Java集合类学习笔记(各种Map实现类的性能分析)

    HashMap和Hashtable的实现机制几乎一样,但由于Hashtable是一个古老的.线程安全的集合,因此HashMap通常比Hashtable要快. TreeMap比HashMap和Hasht ...

  8. [转]Java反射之如何判断类或变量、方法的修饰符(Modifier解析)

    Java针对类.成员变量.方法,有很多修饰符,例如public.private.static.final.synchronized.abstract等,这些修饰符用来控制访问权限或其他特性. 本文就用 ...

  9. CSipSimple通话记录分组

    为了便于查看通话记录,通常要对通话记录进行分组.本质上来说这没什么难度,只需要用ContentResolver去读数据库,剩下的就是策略问题.代码在com/csipsimple/ui/calllog/ ...

  10. C++ TR1 Function Bind

    在C++ 11出现以前,C++的事件一般是通过回调形试来实现,如 void (*func)(int,int,int),其实际上是一种函数指针,在C中调用时是直接写函数名在参数列表中,而在C++中,大部 ...