Square

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 20   Accepted Submission(s) : 12

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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 <stdio.h>
#include<string.h>
int a[];
int vist[];
int sum;
int l;
int n;
int flag;
void Dfs(int t, int len, int index)
{ if (t == )
{
flag = ;
return ;
} if (len == l)
{
Dfs(t + , , );
if (flag)//优化时间
{
return ;
}
} for (int i = index; i < n; i++)//从index开始优化时间
{
if (vist[i]== && a[i] + len <= l)
{
vist[i] = ;
Dfs(t, a[i] + len, i + );
if (flag)//优化时间
{
return;
}
vist[i] = ;
}
}
} int main()
{
int i,t;
scanf("%d", &t);
while (t--)
{ sum = ;
scanf("%d", &n);
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
} if (sum % != )//简答的优化
{
puts("no");
continue;
} l = sum / ; for (i = ; i < n; i++)//有比边长大的边就不行
{
if (a[i] > l)
{
break;
}
}
if (i != n)
{
puts("no");
continue;
}
memset(vist, , sizeof(vist));
flag = ;
Dfs(, , );
if (flag)
{
puts("yes");
}
else
{
puts("no");
}
}
return ;
}

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

  1. HDU 1518 Square(DFS)

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

  2. hdu 1518 Square 木棍建正方形【DFS】

    题目链接 题目大意: 题意就是输入棍子的数量和每根棍子的长度,看能不能拼成正方形. #include <bits/stdc++.h> using namespace std; int n, ...

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

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

  4. HDU 1518 Square 搜索

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

  5. hdu 1518 Square 深搜,,,,花样剪枝啊!!!

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

  6. HDU 1518 Square

    解题思路:sum%4!=0    , max<sum/4 #include<iostream>#include<cstdio>#include<cstring> ...

  7. Square HDU 1518 搜索

    Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...

  8. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  9. hdu Square DFS

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

  10. hdoj 1518 Square 【dfs】

    题意:给出n个(不同长度的)棍子,问能不能将他们构成一个正方形. 策略:深搜. hdoj 1455的简化版 代码: #include <stdio.h> #include <stri ...

随机推荐

  1. @class的基本使用

    2-@class 的基本使用 1, @class的作用 @class 允许简单的引用类,即类的声明.告诉编译器,后面代码中可能会使用到的类名. 好比函数声明一样. 2, #import的作用 与 #i ...

  2. "ERR_GFX_D3D_INIT", GTA5-报错解决办法

    GTA5 PC 版,online模式报错“ERR_GFX_D3D_INIT”. 网上搜到一篇文章,可以解决此问题: http://fixcrasheserrorguide.com/fix-grand- ...

  3. 还在在专业的blog记录生活吧!

    本来觉得自己写文章水平很烂,技术贴也不能保证分析清晰透彻,就决定在百度hi上记录生活随笔.的. 但是,在百度,随便写点啥,都要审核.申诉. 还是在博客园安家吧. 从新手做起.

  4. Codevs 1064 虫食算 2004年NOIP全国联赛提高组

    1064 虫食算 2004年NOIP全国联赛提高组 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 所谓虫食算,就是原先的算式 ...

  5. bzoj1901:Zju2112 Dynamic Rankings

    思路:树套树,我写了两种,一种是线段树套splay,线段树维护区间信息,splay维护第k大,一种是树状数组套权值线段树(并不是什么可持久化线段树,只不过是动态开点罢了,为什么网上一大堆题解都是可持久 ...

  6. 九度OJ 1066 字符串排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1066 题目描述: 输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出 ...

  7. 基于NodeJs的网页爬虫的构建(一)

    好久没写博客了,这段时间已经忙成狗,半年时间就这么没了,必须得做一下总结否则白忙.接下去可能会有一系列的总结,都是关于定向爬虫(干了好几个月后才知道这个名词)的构建方法,实现平台是Node.JS. 背 ...

  8. [C#]『Task』任务并行库使用小计

    1.简单创建使用 using System; using System.Diagnostics; using System.Threading; using System.Threading.Task ...

  9. python連接mysql數據庫

    第一步,安裝mysql數據庫. 這裏我安裝的是mariadb數據庫,版本5.5,並且配置好了字符集.此處不詳細敘述,相信大家沒有問題. 第二步,安裝mysql驅動. 首先說明一下有兩個主要的驅動: m ...

  10. php内存申请和销毁

    内存申请 ZendMM使用自身heap层申请内存追踪结果: ZEND_ASSIGN_SPEC_CV_CONST_HANDLER (......) -> ALLOC_ZVAL(......) -& ...