HDU1518 Square
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define max(a, b)(a > b ? a : b)
#define N 30 int a[N], vis[N], n, k, f; void DFS(int s, int m, int d)//s为搜索起点 m为已组成的正方体的边数 d当前组成木棍的长度(正方体的一条边可由多个木棍组成)
{
int i;
if(m == )
{
f = ;
return ;
}
if(d == k)
DFS(, m + , );
for(i = s ; i < n ; i++)
{
if(!vis[i] && d + a[i] <= k)
{
vis[i] = ;
DFS(i + , m, d + a[i]);
vis[i] = ;
if(f == )
return ;
}
}
}
int main()
{
int t, sum, max, i;
scanf("%d", &t);
while(t--)
{
sum = f = max = ;
scanf("%d", &n);
for(i = ; i < n ; i++)
{
scanf("%d", &a[i]);
sum += a[i];
max = max(max, a[i]);
}
if(sum % != || max > sum / || n < )//正方体的边数为小数,长度最大的木棍大于正方体的边数。木棍总数小于4都不能组成正方体
printf("no\n");
else
{
k = sum / ;
memset(vis, , sizeof(vis));
DFS(, , );
if(f == )
printf("yes\n");
else
printf("no\n");
}
}
return ;
}
HDU1518 Square的更多相关文章
- HDU1518 Square(DFS,剪枝是关键呀)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- HDU1518 Square(DFS)
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- HDU-1518 Square(DFS)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- HDU1518:Square(DFS)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- 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 ...
- HDU1518 Square 【剪枝】
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- Nginx+Tomcat+Memcached 集群
写过一篇 Apache Httpd+Tomcat 的集群, 现在来看看Nginx+Tomcat+Memcached是怎么集群的. 先看看用的版本和工具: Nginx: nginx-1.8.1 Tomc ...
- 5 commands to check memory usage on Linux
Memory Usage On linux, there are commands for almost everything, because the gui might not be always ...
- Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1
Reference link: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-nu ...
- HDU 2594 (简单KMP) Simpsons’ Hidden Talents
题意: 有两个字符串,找一个最长子串是的该串既是第一个字的前缀,又是第二个串的后缀. 分析: 把两个串并起来然后在中间加一个无关字符,求next数组即可. #include <cstdio> ...
- UVa 12304 (6个二维几何问题合集) 2D Geometry 110 in 1!
这个题能1A纯属运气,要是WA掉,可真不知道该怎么去调了. 题意: 这是完全独立的6个子问题.代码中是根据字符串的长度来区分问题编号的. 给出三角形三点坐标,求外接圆圆心和半径. 给出三角形三点坐标, ...
- tomcat 默认项目设置
正常情况下,我们启动tomcat后,直接输入“http://localhost:端口/“ 后,默认访问道是webapp目录下的ROOT应用. 我们要通过上述方式访问自己的应用,有俩种方式. 第一:把自 ...
- LeetCode: Sqrt
Title: Implement int sqrt(int x). Compute and return the square root of x. 思路:这个平方根肯定是在[1,x]之间,所以在这个 ...
- POJ 1833 排列
题意: 给你某个排列 求从下一个排列开始的第k个排列如果是最后一个排列 则下一个排列为1 2 3 ... n// 1 用stl 里面的 next_permutation// 2 用生成下一个排列算法/ ...
- 常用的css的技巧
1.在做项目当中,由静态页面来载入到项目中,作为动态数据的部分,若是这个动态数据,前面或者后面有需要图片显示(图片是用background来显示的),一般不用float:left或者right,而是p ...
- C#的switch与二维数组.....
今天由于工作上的需要, 改了几行C# 的代码, 发现有一些细微的语法区别,与C++, 像switch语句那样, 我一般不会在default后面加上break,语句, 可是发现如果不加上的话,就会报下 ...