HDU 5113 dfs剪枝
题意:告诉格子规格,颜色个数,以及每个颜色能涂得格子数目,问是否能够实现相邻两个格子的颜色数目不相同。
分析:因为数据很小,格子最多是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剪枝的更多相关文章
- 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 ...
- hdu 4109 dfs+剪枝优化
求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己添加的)出发,0到1~n个节点之间的距离为1.mt[i]表示从0点到第i个节点眼下所得的最长路径 #include<iost ...
- Tempter of the Bone HDU 1010(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- hdu 1518 dfs+剪枝
题目大意:几根棒子能否组成一个正方形 Sample Input3 //测试组数4 1 1 1 1 //棒子数目以及每根棒子的长度5 10 20 30 40 508 1 7 2 ...
- 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 ...
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...
- HDU 5113
HDU 5113类似四色定理的什么东西,大体就是dfs了,不过有两个坑点,这个题的逼格瞬间就上去了1.剪枝很神奇,任何一种颜色都不能超过剩下总格子数的一半,想想确实显然但是比赛的时候没有想到:2.测评 ...
随机推荐
- 破解YunFile下载间隔10分钟/下载等待30秒
[破解10分钟间隔] 可以采用断网重连等方法重新获取IP地址,就不用再等十分钟了 [破解30秒等待] 收藏一个书签,书签地址如下 javascript:var downpage_link = docu ...
- 关于rem的学习
网页常见单位px.em.rem:其中rem是css3新出的单位,官网是这样定义的:“font size of the root element”,意思是:相对长度单位,相对于根元素(即html元素)f ...
- Centos7 hostname重启失效
/etc/hosts 文件如下 [root@god ~]# more /etc/hosts 127.0.0.1 localhost ::1 localhost localhost.localdoma ...
- ORACLE 连接SQLSERVER 数据库备忘
最近工作需要,要从SQL SERVER数据库中同步提取数据. 这里采用了 Oracle Gateway 来连接,折腾了半天,终于搞定,记录下已备下次使用. 基本资料网上都可以搜很多,官网配置说明在这 ...
- NoSQl简介(转)
NoSQL,全称是“Not Only Sql”,指的是非关系型的数据库.这类数据库主要有这些特点:非关系型的.分布式的.开源的.水平可扩展的.原始的目的是为了大规模web应用,这场全新的数据库革命运动 ...
- STEP模块——电子钟
原理 显示时分秒(日期也可以加上),两个按键调节时间 原理图 代码 /*---------------------------------------------------------------- ...
- Redux教程3:添加倒计时
前面的教程里面,我们搭建了一个简单红绿灯示例,通过在console输出当面的倒计时时间:由于界面上不能显示倒计时,用户体验并不良好,本节我们就添加一个简单的倒计时改善一下. 作为本系列的最后一篇文章, ...
- Android中的数据保存
形式 Android的数据保存分为3种形式:file, SharedPreference, Database 文件 主要思想就是通过Context类中提供的openFileInput和openFile ...
- string类实现(C++)
class CMyString { friend std::ostream& operator<<( std::ostream& os, const CMyString&a ...
- Spark机器学习读书笔记-CH05
5.2.从数据中提取合适的特征 [root@demo1 ch05]# sed 1d train.tsv > train_noheader.tsv[root@demo1 ch05]# lltota ...