1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5. #define max(a, b)(a > b ? a : b)
  6. #define N 30
  7.  
  8. int a[N], vis[N], n, k, f;
  9.  
  10. void DFS(int s, int m, int d)//s为搜索起点 m为已组成的正方体的边数 d当前组成木棍的长度(正方体的一条边可由多个木棍组成)
  11. {
  12. int i;
  13. if(m == )
  14. {
  15. f = ;
  16. return ;
  17. }
  18. if(d == k)
  19. DFS(, m + , );
  20. for(i = s ; i < n ; i++)
  21. {
  22. if(!vis[i] && d + a[i] <= k)
  23. {
  24. vis[i] = ;
  25. DFS(i + , m, d + a[i]);
  26. vis[i] = ;
  27. if(f == )
  28. return ;
  29. }
  30. }
  31. }
  32. int main()
  33. {
  34. int t, sum, max, i;
  35. scanf("%d", &t);
  36. while(t--)
  37. {
  38. sum = f = max = ;
  39. scanf("%d", &n);
  40. for(i = ; i < n ; i++)
  41. {
  42. scanf("%d", &a[i]);
  43. sum += a[i];
  44. max = max(max, a[i]);
  45. }
  46. if(sum % != || max > sum / || n < )//正方体的边数为小数,长度最大的木棍大于正方体的边数。木棍总数小于4都不能组成正方体
  47. printf("no\n");
  48. else
  49. {
  50. k = sum / ;
  51. memset(vis, , sizeof(vis));
  52. DFS(, , );
  53. if(f == )
  54. printf("yes\n");
  55. else
  56. printf("no\n");
  57. }
  58. }
  59. return ;
  60. }

HDU1518 Square的更多相关文章

  1. HDU1518 Square(DFS,剪枝是关键呀)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  2. HDU1518 Square(DFS)

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

  3. HDU-1518 Square(DFS)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  4. HDU1518:Square(DFS)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

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

  6. HDU1518 Square 【剪枝】

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

  7. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  8. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  9. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

随机推荐

  1. Android 面试题(经典)

    1.Actvity的生命周期,生命周期中的onCreate与onResume有什么区别 Activity的生命周期有:onCreate,onStart,onRestart,onResume,onPau ...

  2. java 语言里 遍历 collection 的方式

    我来简单说一下吧,一般有2种方法来遍历collection中的元素,以HashSet为例子HashSet hs=new HashSet();hs.add("hello");hs.a ...

  3. 用net匹配并替换iOS标准的emoji表情符号

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespaceCommo ...

  4. QSettings介绍

    简介 QSettings类提供了持久的跨平台应用程序设置. 用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表,OS X和iOS的属性列表文件中. ...

  5. UVa 116 (多段图的最短路) Unidirectional TSP

    题意: 有一个m行n列的正整数环形矩阵(即矩阵第一行的上一行是最后一行,最后一行的下一行是第一行),从第一列的任意位置出发,每次只能向右,右上,右下三个方向行走,输出路径及路径上所有数之和的最大值,多 ...

  6. BNU 4188 Superprime Rib【BFS】

    题意:给出n,输出n位超级质数,超级质数的定义为“依次去掉右边一位后仍然为质数的数” 因为一个n位质数去掉右边一位数之后仍然为质数,说明它是由n-1位超级质数演变而来的, 同理,n-1位超级质数也由n ...

  7. Java知识点:琐碎知识点(2)

    49个关键字一览 abstract default if private this boolean do implements protected throw break double import ...

  8. Java [Leetcode 191]Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

  9. ASP.NET服务器控件对应的HTML标签

    了解ASP.NET的控件最终解析成什么HTML代码,有助于我们对ASP.NET更深的了解,在使用JS交互时也知道如何操作. ASP.NET 服务器控件渲染到客户端之后对应的HTML标签讲解. labe ...

  10. loadrunner 发送gzip压缩json格式(转)

    转:http://blog.csdn.net/gzh0222/article/details/7711281 使用java vuser实现,发送gzip压缩json格式. /* * LoadRunne ...