题目描述:

Problem Description
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
Input
The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
Output
For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
Sample Input
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

Sample Output
yes
no
yes
代码如下:
 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)相关推荐

  1. hdu 1511(dp)

    转载标记处:http://blog.csdn.net/my_acm_dream/article/details/41577645 解题思路:两次dp确实很巧妙,我只想到了一次dp算出最后那个数最小,其 ...

  2. Digital Square HDU - 4394 dfs + 剪枝

    一.内容 Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow ...

  3. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  4. H - Square Card HDU - 7063

    H - Square Card HDU - 7063 题意: 有两个圆形区域,一个是得分区域,一个是获得奖金区域,现在你有一个边长为a的正方形,当正方形在如果在某一时刻它严格在圆形范围内,才算合法. ...

  5. hdu 5079 Square

    http://acm.hdu.edu.cn/showproblem.php?pid=5079 题意: n*n网格,每个格子可以涂黑色或白色,有的格子必须涂黑色 问最大白色正方形边长分别为0,1,2,- ...

  6. 【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 ...

  7. 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 ...

  8. HDU 1398 Square Coins

    母函数简单应用 题目: Square Coins Problem Description People in Silverland use square coins. Not only they ha ...

  9. HDU 1398 Square Coins

    题目大意:有面值分别为.1,4,9,.......17^2的硬币无数多个.问你组成面值为n的钱的方法数. 最简单的母函数模板题: #include <cstdio> #include &l ...

  10. hdu 5903 Square Distance

    这题题解dp不懂 因为不知道它怎么记录dp的答案的 字符串那么长 我是贪心过得,当时还被四个人hack,都没成功,hhhhh 大意就是优先从头取字典序小的字母,担也要让后面不管怎么取都合法 #incl ...

最新文章

  1. paoding java_中文分词器-PaodingAnalyzer
  2. Java如何转换protobuf-net中的bcl.Decimal对象
  3. Flutter 异常处理之图片篇
  4. [另开新坑] 算导v3 #26 最大流 翻译
  5. 降低深度学习开发门槛,“动态图+高层API”能带来多大的便利?
  6. wdcp 无法更换php,wdcp降级到php5.2后Zend Optimizer失效的解决办法
  7. rto净化效率计算公式_旋转式RTO,催化燃烧设备,在各行业的应用及优势
  8. 暑假快来了,又该何去何从
  9. 开源正在蚕食 500 亿美元的数据库行业!
  10. 16种常用的数据分析方法-聚类分析
  11. Java为什么要面向接口编程
  12. 笔记本硬盘调研 更换及启动盘设置
  13. 【分布式事务】如何基于消息中间件实现分布式事务?万字长文给你答案!!
  14. 2022年4月语音合成(TTS)和语音识别(ASR)论文月报
  15. access两位小数不进位_文章列表-火龙的博客 - PHP,GO,MySQL知识分享问题记录博客...
  16. 爱奇艺qsv格式视频无损转换为MP4
  17. 7-12 打印倒直角三角形图案
  18. Android调用相机预览黑屏app passed NULL surface解决
  19. CSS - Tooltip-arrow 绘制三角形
  20. 文本域默认会放大缩小,如何把文本域设置为禁止推拽状态,从而固定大小呢

热门文章

  1. lanmp_wdcp_v2.4快速安装RPM包发布
  2. Delphi - 注入的方式来禁止任务管理器
  3. Linux爆本地提权漏洞 请立即更新udev程序
  4. DNN永日新闻模块(YongRi)免费1.00.09版本下载
  5. Django路由控制
  6. 深入理解java集合框架之---------Arraylist集合 -----添加方法
  7. 洛谷 p1434 滑雪【记忆化搜索】
  8. 浏览器宽度和高度的说明
  9. VS2015自带的LocalDB数据库的用法
  10. debian软raid