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

分析:因为数据很小,格子最多是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. 转,Oracle中关于处理小数点位数的几个函数,取小数位数,Oracle查询函数

    关于处理小数点位数的几个oracle函数() 1. 取四舍五入的几位小数 select round(1.2345, 3) from dual; 结果:1.235 2. 保留两位小数,只舍 select ...

  2. 16090202(剑灵GPA)

    [目标] 剑灵GPA [思路] 1 2 绘制角色DrawCall body 5526面片 2.1[第一个DrawCall]63 RT 这个DrawCall PS VS 参数列表 VS // // Ge ...

  3. Rendering Problems:android.support.v7.internal.widget.ActionBarOverlayLayout 解决方法

    不知道是不是android studio安装不对的问题,每次新建项目都有这个问题. 临时解决方法是: 打开 styles.xml 在Theme.AppCompat.Light.DarkActionBa ...

  4. UIScrollView无法滚动的解决办法及UIScrollView的代理(delegate)

    1•如果UIScrollView无法滚动,可能是以下原因: Ø没有设置contentSize ØscrollEnabled = NO Ø没有接收到触摸事件:userInteractionEnabled ...

  5. twrp编译步骤 (CWM也差不多)

    1.源码的跟目录执行:. build/envsetup.sh,构建编译环境 2.执行 make -j4 otatools,生成后面编译需要的工具 3.把boot或recovery放到home目录,执行 ...

  6. SpringMVC 文件上传&拦截器&异常处理

    文件上传 Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的.Spring 用 Jakarta Commons FileUpload ...

  7. adb shell

    1.获取进程ID adb shell ps |findstr packagename 2.获取cpu的值 adb shell dumpsys cpuinfo |findstr packagename ...

  8. Apache Flume 1.7.0 发布,日志服务器

    Apache Flume 1.7.0 发布了,Flume 是一个分布式.可靠和高可用的服务,用于收集.聚合以及移动大量日志数据,使用一个简单灵活的架构,就流数据模型.这是一个可靠.容错的服务. 本次更 ...

  9. 对客户推荐产品模型+python代码

    首先观看数据: l  数据的基本特征用  describe  描述每个基本特征 l  画图画出每个特征的基本统计图 应用import matplotlib.pylab as pl  画图显示 l  关 ...

  10. centos7安装

    1.准备工具 VMware,我用的是 VMware11 2.打开VMware,创建新的虚拟机 3.选择典型-->下一步 4.稍后安装操作系统-->下一步 5.选择linux操作系统,lin ...