Square(hdu 1511)
题目描述:
1 /*要剪枝,当所有木棒总长度不能被4整除时以及木棒最大长度大于总长度除以4时, 2 * 不能组成正方形,直接输出no 3 * 深搜时从第一个开始往后搜索,只要满足当前边长+当前木棒长<正方形边长, 4 * 就标记该木棒,并继续搜索后面的木棒,当木棒长度=sum/4 时,count加1, 5 * 当count=3时表明能够成正方形,flag=1,返回,flag=0则不能组成正方形。*/ 6 #include<iostream> 7 #include<cstring> 8 9 int visit[20]; 10 bool flag; 11 int number[20]; 12 int n,len; 13 14 using namespace std; 15 16 void dfs(int cur,int pos,int count); 17 int main() 18 { 19 int m; 20 cin >> m; 21 while(m--) 22 { 23 int max = 0,sum = 0; 24 cin >> n; 25 memset(visit,0,sizeof(visit)); 26 for(int i = 0;i < n;i++) 27 { 28 cin >> number[i]; 29 sum += number[i]; 30 if(number[i] > max) 31 max = number[i]; 32 } 33 len = sum / 4; 34 if(sum % 4 != 0 || max > len)//不能总和不能被4整除或最大值大于平均值 35 { 36 cout << "no" << endl; 37 continue; 38 } 39 flag = 0; 40 dfs(0,0,0); 41 if(flag) 42 cout << "yes" << endl; 43 else 44 cout << "no" << endl; 45 } 46 } 47 48 void dfs(int cur,int pos,int count) 49 { 50 if(cur == len)//木棍相加的值等于平均值 51 { 52 count++; 53 cur = 0;//当一边成立时,要考虑另外一边注意将cur清0 54 pos = 0; 55 if(count == 3)//当有三边成立时 56 { 57 flag = 1; 58 return; 59 } 60 } 61 for(int i = pos;i < n;i++) 62 { 63 if(!visit[i])//还没有被访问过 64 { 65 if((cur + number[i]) <= len)//加上木棍后,长度小于或等于平均值 66 { 67 visit[i] = 1; 68 dfs(cur + number[i],i,count); 69 if(flag)//一旦有成立的,跳过剩下的判断 70 return; 71 visit[i] = 0;//回溯 72 } 73 } 74 } 75 }
代码分析:
这道题目要注意的地方就是剪枝。
参考地址:http://www.cnblogs.com/PegasusWang/archive/2013/04/08/3008942.html
转载于:https://www.cnblogs.com/linxiaotao/p/3451851.html
Square(hdu 1511)相关推荐
- hdu 1511(dp)
转载标记处:http://blog.csdn.net/my_acm_dream/article/details/41577645 解题思路:两次dp确实很巧妙,我只想到了一次dp算出最后那个数最小,其 ...
- Digital Square HDU - 4394 dfs + 剪枝
一.内容 Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow ...
- 杭电OJ分类题目(1)
原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...
- H - Square Card HDU - 7063
H - Square Card HDU - 7063 题意: 有两个圆形区域,一个是得分区域,一个是获得奖金区域,现在你有一个边长为a的正方形,当正方形在如果在某一时刻它严格在圆形范围内,才算合法. ...
- hdu 5079 Square
http://acm.hdu.edu.cn/showproblem.php?pid=5079 题意: n*n网格,每个格子可以涂黑色或白色,有的格子必须涂黑色 问最大白色正方形边长分别为0,1,2,- ...
- 【HDU - 1518】Square (经典的dfs + 剪枝)
题干: Given a set of sticks of various lengths, is it possible to join them end-to-end to form a squar ...
- hdu 1398 Square Coins/hdu 1028 Ignatius and the Princess III
两道母函数的模板题: http://acm.hdu.edu.cn/showproblem.php?pid=1398 View Code #include<iostream>#include ...
- HDU 1398 Square Coins
母函数简单应用 题目: Square Coins Problem Description People in Silverland use square coins. Not only they ha ...
- HDU 1398 Square Coins
题目大意:有面值分别为.1,4,9,.......17^2的硬币无数多个.问你组成面值为n的钱的方法数. 最简单的母函数模板题: #include <cstdio> #include &l ...
- hdu 5903 Square Distance
这题题解dp不懂 因为不知道它怎么记录dp的答案的 字符串那么长 我是贪心过得,当时还被四个人hack,都没成功,hhhhh 大意就是优先从头取字典序小的字母,担也要让后面不管怎么取都合法 #incl ...
最新文章
- paoding java_中文分词器-PaodingAnalyzer
- Java如何转换protobuf-net中的bcl.Decimal对象
- Flutter 异常处理之图片篇
- [另开新坑] 算导v3 #26 最大流 翻译
- 降低深度学习开发门槛,“动态图+高层API”能带来多大的便利?
- wdcp 无法更换php,wdcp降级到php5.2后Zend Optimizer失效的解决办法
- rto净化效率计算公式_旋转式RTO,催化燃烧设备,在各行业的应用及优势
- 暑假快来了,又该何去何从
- 开源正在蚕食 500 亿美元的数据库行业!
- 16种常用的数据分析方法-聚类分析
- Java为什么要面向接口编程
- 笔记本硬盘调研 更换及启动盘设置
- 【分布式事务】如何基于消息中间件实现分布式事务?万字长文给你答案!!
- 2022年4月语音合成(TTS)和语音识别(ASR)论文月报
- access两位小数不进位_文章列表-火龙的博客 - PHP,GO,MySQL知识分享问题记录博客...
- 爱奇艺qsv格式视频无损转换为MP4
- 7-12 打印倒直角三角形图案
- Android调用相机预览黑屏app passed NULL surface解决
- CSS - Tooltip-arrow 绘制三角形
- 文本域默认会放大缩小,如何把文本域设置为禁止推拽状态,从而固定大小呢