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

  1. 3
  2. 4 1 1 1 1
  3. 5 10 20 30 40 50
  4. 8 1 7 2 6 4 4 3 5

Sample Output

  1. yes
  2. no
  3. yes

Source

University of Waterloo Local Contest 2002.09.21
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. int m,edge;//木棍的个数,木棍的边长
  5. int stick[];//木棍的长度
  6. bool vis[];//是否用过木棍
  7. bool flag;
  8. void dfs(int n,int len,int i)//n是第几条边,len是这条边已有长度,i从第几条边开始查找(防止超时)
  9. {
  10. if(n==)//终止条件
  11. {
  12. flag=true;
  13. return;
  14. }
  15. if(len==edge)
  16. {
  17. dfs(n+,,);
  18. if(flag==true)
  19. return ;
  20. }
  21. for(int j=i;j<=m;j++)
  22. {
  23. if(!vis[j])
  24. {
  25. if(stick[j]+len<=edge)
  26. {
  27. vis[j]=true;
  28. dfs(n,len+stick[j],j+);
  29. if(flag==true)
  30. return ;
  31. vis[j]=false;
  32. }
  33. }
  34. }
  35.  
  36. }
  37. int main()
  38. {
  39. int T;
  40. cin>>T;
  41. while(T--)
  42. {
  43. cin>>m;
  44. edge=;
  45. for(int i=;i<=m;i++)
  46. {
  47. cin>>stick[i];
  48. edge+=stick[i];
  49. }
  50. if(edge%!=)//第一个剪枝:是否能组成正方形
  51. {
  52. printf("no\n");
  53. continue;
  54. }
  55. edge/=;
  56. int i;
  57. for(i=;i<=m;i++)//第二个剪枝:木棍小于等于边长
  58. {
  59. if(stick[i]>edge)
  60. break;
  61. }
  62. if(i!=m+)
  63. {
  64. printf("no\n");
  65. continue;
  66. }
  67. memset(vis,false,sizeof(vis));
  68. flag=false;
  69. dfs(,,);
  70. if(flag)
  71. printf("yes\n");
  72. else
  73. printf("no\n");
  74. }
  75. return ;
  76. }

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. pip 安装pandas报UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5错

    当Python在window环境中通过pip安装pandas报标题这样的错,主要是因为python默认编码格式是:ascii 在https://www.python.org/dev/peps/pep- ...

  2. 深入理解Hadoop之HDFS架构

    Hadoop分布式文件系统(HDFS)是一种分布式文件系统.它与现有的分布式文件系统有许多相似之处.但是,与其他分布式文件系统的差异是值得我们注意的: HDFS具有高度容错能力,旨在部署在低成本硬件上 ...

  3. Android移动应用界面的模板化设计

    Android没有像苹果开发那样功能强大的界面开发工具,本身 ADT插件提供的界面编辑能力有限,没办法刻画所有的界面情况:Android的界面xml代码可以进行人工修改,而Iphone的全部在图形界面 ...

  4. springboot入门(一)--快速搭建一个springboot框架

    原文出处 前言在开始之前先简单介绍一下springboot,springboot作为一个微框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...

  5. [Java学习] Java instanceof 运算符

    多态性带来了一个问题,就是如何判断一个变量所实际引用的对象的类型 . C++使用runtime-type information(RTTI),Java 使用 instanceof 操作符. insta ...

  6. Ivan and Burgers CodeForces - 1100F (线性基)

    大意: 给定n元素序列, m个询问$(l,r)$, 求$[l,r]$中选出任意数异或后的最大值 线性基沙茶题, 直接线段树暴力维护两个log还是能过的 #include <iostream> ...

  7. python-day63--前端

    一. 响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现不同的布局等 - 响应式怎么实现的? 1. CSS3 media query 媒体查询 2. JS去控制网页的布局和样式等 - 缺 ...

  8. python-day53--前端js

    一.基本语法(ECMA) 单行注释 // /* 多行注释 */ 变量赋值 默认以换行符作为结束符,有分好以分号作为结束符号 JS的引入方式: 1. <script> </script ...

  9. python-day43--多表查询

    一.多表连接查询:       #重点:外链接语法 准备表 #建表 create table department( id int, name varchar(20) ); create table ...

  10. python-day10--字符编码

    1.回顾: 软件→操作系统→硬件 2.文本编辑器: 启动:硬盘→内存→运行(cpu) 读文件:硬盘→内存→CPU读 存文件:保存到硬盘中 3.python解释器 启动:硬盘→内存→运行(cpu) 读文 ...