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. 常用Gene ID转换工具--转载

    在自己的研究工作中,经常会遇到一些需要对Gene ID进行转换的情况.目前存在着大量的生物信息数据库,每个数据库都有自己定义的ID命名规则,转换起来实在是一个很大的工作.举个例子,之前构建的Human ...

  2. shell 输出双引号

    #!/bin/sh your_name='runoob' str="Hello, I know you are \"$your_name\"! \n" echo ...

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

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

  4. Flutter学习笔记(二)

    *.assets 当引用图片的时候,需要在pubspec.yaml的文件中的flutter下添加assets,类似于下面的样子: image.png 这里需要注意的是文件里的assets只要一个缩进即 ...

  5. java后台校验 hibernate validator

    链接 : https://www.cnblogs.com/softidea/p/6044123.html

  6. Freemarker 简介

    1.动态网页和静态网页差异 在进入主题之前我先介绍一下什么是动态网页,动态网页是指跟静态网页相对应的一种网页编程技术.静态网页,随着HTML代码的生成,页面的内容和显示效果就不会再发生变化(除非你修改 ...

  7. [Java代码] Java是自学好还是参加培训班好?

    ava 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言. Java可运行于多个平台,如Windows, Mac OS,及其他多种UNIX版本的系统. 本教程给大家简单介 ...

  8. PHP面向对象初中高级之由浅入深

    php面向对象编程基本实践:(了解类,类到对象的实例化,构造和析构,对象的引用); 类的概念: 物以类聚,把具有相似特性的对象对垒到一个类中 类定义了这些相似对象拥有的相同的属性和方法 类是相似对象的 ...

  9. py to exe —— pywin32

    xu言: 最近研究python,觉得做些windows小工具还挺好玩,就研究了下py代码如何转成exe 这里用到一个工具 pywin32 https://sourceforge.net/project ...

  10. wacher和acl

      一.wacher    问题      1.集群中有多个机器,当某个通用的配置发生变化 ,怎么让所有服务器的配置都统一生效?       2.当某个集群节点宕机,其它节点怎么知道?   Zk中引入 ...