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

Source

University of Waterloo Local Contest 2002.09.21
 #include<iostream>
#include<cstring>
using namespace std;
int m,edge;//木棍的个数,木棍的边长
int stick[];//木棍的长度
bool vis[];//是否用过木棍
bool flag;
void dfs(int n,int len,int i)//n是第几条边,len是这条边已有长度,i从第几条边开始查找(防止超时)
{
if(n==)//终止条件
{
flag=true;
return;
}
if(len==edge)
{
dfs(n+,,);
if(flag==true)
return ;
}
for(int j=i;j<=m;j++)
{
if(!vis[j])
{
if(stick[j]+len<=edge)
{
vis[j]=true;
dfs(n,len+stick[j],j+);
if(flag==true)
return ;
vis[j]=false;
}
}
} }
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>m;
edge=;
for(int i=;i<=m;i++)
{
cin>>stick[i];
edge+=stick[i];
}
if(edge%!=)//第一个剪枝:是否能组成正方形
{
printf("no\n");
continue;
}
edge/=;
int i;
for(i=;i<=m;i++)//第二个剪枝:木棍小于等于边长
{
if(stick[i]>edge)
break;
}
if(i!=m+)
{
printf("no\n");
continue;
}
memset(vis,false,sizeof(vis));
flag=false;
dfs(,,);
if(flag)
printf("yes\n");
else
printf("no\n");
}
return ;
}

HDU 1518 Square(DFS)的更多相关文章

  1. HDU 5965 扫雷(dfs)题解

    题意:给你一个3*n的格子,中间那行表明的是周围8格(当然左右都没有)的炸弹数量,上下两行都可以放炸弹,问你有几种可能,对mod取模 思路:显然(不),当i - 1和i - 2确定时,那么i的个数一定 ...

  2. hdu 1518 Square(深搜+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...

  3. HDU 1015 Safecracker (DFS)

    题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...

  4. HDU1518 Square(DFS)

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  5. Hdu 1175 连连看(DFS)

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...

  6. HDU1518 Square(DFS) 2016-07-24 15:08 49人阅读 评论(0) 收藏

    Square Problem Description Given a set of sticks of various lengths, is it possible to join them end ...

  7. hdu 2821 Pusher (dfs)

    把这个写出来是不是就意味着把   http://www.hacker.org/push  这个游戏打爆了? ~啊哈哈哈 其实只要找到一个就可以退出了  所以效率也不算很低的  可以直接DFS呀呀呀呀 ...

  8. hdu 2821 Pusher(dfs)

    Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, ...

  9. HDU 1501 Zipper(DFS)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

随机推荐

  1. workerman如何写mysql连接池

    首先要了解为什么用连接池,连接池能为你解决什么问题 连接池主要的作用1.减少与数据服务器建立TCP连接三次握手及连接关闭四次挥手的开销,从而降低客户端和mysql服务端的负载,缩短请求响应时间2.减少 ...

  2. 安装labelImage的问题qt

    不知为何需要些source ~/.bash_profile才 自己在 ~/.bash_profile 中配置环境变量, 可是每次重启终端后配置的不生效.需要重新执行 : $source ~/.bash ...

  3. MongoVUE的Collections数据不显示的解决方法

    问题描述: 使用 mongoDB数据库, 数据添加成功了,使用命令行能查询出来,但在MongoVUE 中数据却不显示  (我使用的是 mongoDB 3.4 的版本) 原因:引擎问题,只要降到2.X版 ...

  4. Hadoop 部分截图

  5. Struts2文件上传的大小限制问题

      问题:上传大文件报错…… 解决:修改struts.xml文件中的参数如下 <constant name="struts.multipart.maxSize" value= ...

  6. Qt4_WebKit_例子

    1. 安装包:qt-opensource-windows-x86-vs2010-4.8.6.exe Qt4已经编译好的文件:E:\ZC_software_installDir\Qt_4.8.6\dem ...

  7. 《剑指offer》第二十一题(调整数组顺序使奇数位于偶数前面)

    // 面试题21:调整数组顺序使奇数位于偶数前面 // 题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有 // 奇数位于数组的前半部分,所有偶数位于数组的后半部分. #inclu ...

  8. Caffe 学习系列

    学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...

  9. C# DataTable按指定列排序

    C#提供的内置对象DataTable功能特别的强大,如果我们需要对DataTable中的某一列进行排序怎么处理呢,具体代码如下: DataTable dt = new DataTable(); dt. ...

  10. Lunar New Year and Red Envelopes CodeForces - 1106E (dp)

    大意: 总共$n$的时间, $k$个红包, 红包$i$只能在时间$[s_i,t_i]$范围内拿, 并且拿完后时间跳到$d_i+1$,Bob采用贪心策略,每个时间点若有红包能取则取钱数$w_i$最大的, ...