Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5604    Accepted Submission(s): 1776

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
/*
1优化
如果按照素数环的方法,是暴搜的。超。
当第一次满足if(p==sum)的时候,(1).不需要再继续搜索下去,(2).而且从头再开始搜。
要满足(1),(2)我们在DFS中设立两个相应的变量n,p;
if(sum=p+f[i]) dfs(0,0,cur+1);
else if(p+f[i]<sum) dfs(i,p+f[i],cur);
if(p==0) break;
这样话,能有效的处理满足 cur++的情况时,退出当前的搜索。
另外当数字相同的时候也处理,while(f[i]==f[i+1]) i++;
2优化
如果排序,从大到小进行选择数字,避免了随机,能提高速度。
3优化
if(sum%4!=0) no
else sum=sum/4;
cur只要满足 3 就可以了,不需要4。
*/
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
int f[];
int visit[];
int flag;
int sum;
int m;
bool cmp(int a,int b)
{
return a>b;
}
void dfs(int n,int p,int cur)
{
int i;
if(cur==)
{
flag=;
return;
}
if(flag==) return;
for(i=n+;i<=m;i++)
{
if(visit[i]== && flag==)
{
visit[i]=;
if(p+f[i]==sum)
dfs(,,cur+);
else if(p+f[i]<sum)
dfs(i,p+f[i],cur);
visit[i]=;
if(p==) break;
while(f[i]==f[i+]) i++;
}
}
}
int main()
{
int i,j,n;
while(scanf("%d",&n)>)
{
while(n--)
{
scanf("%d",&m);
sum=;
for(i=;i<=m;i++)
{
scanf("%d",&f[i]);
sum=sum+f[i];
}
if(sum%!=)
{
printf("no\n");
continue;
}
sort(f+,f++m,cmp);
for(j=;j<=m;j++)
visit[j]=;
flag=;sum=sum/;
dfs(,,);
if(flag==) printf("yes\n");
else printf("no\n");
}
}
return ;
}

hdu Square DFS的更多相关文章

  1. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  2. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  3. Oil Deposits HDU - 1241 (dfs)

    Oil Deposits HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting undergr ...

  4. hdu 1312(DFS)

    Red and Black Tme Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

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

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

  6. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  7. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  8. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  9. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

随机推荐

  1. A Tool To Plot Mathematical Function

    Plot.cs using Microsoft.ClearScript; using Microsoft.ClearScript.V8; using Microsoft.Win32; using Sy ...

  2. win10中shift+右键,在此处打开cmd窗口

    通过添加注册表项,实现右击“在此处打开命令行功能” 注册表位置:HKEY_CLASSES_ROOT\Directory\Background\shell\ win10系统用标识右键菜单打开命令行的键, ...

  3. 873. Length of Longest Fibonacci Subsequence

    A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...

  4. 《Python绝技:运用Python成为顶级黑客》 用Python实现免杀

    1.免杀的过程: 使用Metasploit生成C语言风格的一些shellcode作为载荷,这里使用Windows bindshell,功能为选定一个TCP端口与cmd.exe进程绑定在一起,方便攻击者 ...

  5. LINUX中如何查看某个端口是否被占用

    之前查询端口是否被占用一直搞不明白,问了好多人,终于搞懂了,现在总结下: 1.netstat  -anp  |grep   端口号 如下,我以3306为例,netstat  -anp  |grep   ...

  6. eclispe中使用python库 pyswip 进行prolog编程

    from pyswip import Prolog prolog = Prolog() prolog.assertz("father(michael,john)") prolog. ...

  7. linux 中 iptables关于ping的问题

    允许其他机器ping通防火墙 iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT iptables -A OUTPUT -p ic ...

  8. (转)使用 DB2 HADR 选择用于灾难恢复的 SUPERASYNC 模式

    使用 DB2 HADR 选择用于灾难恢复的 SUPERASYNC 模式 Vishnu G 和 Hemant Singh2013 年 6 月 25 日发布 WeiboGoogle+用电子邮件发送本页面 ...

  9. Exception message: /bin/bash: line 0: fg: no job control

    这个错误是 我本地idea 远程调试hadoop集群出现的 Diagnostics: Exception from container-launch. Container id: container_ ...

  10. Elasticsearch入门(一)

    索引(index) -- 存储关联数据的地方.实际上,索引只是一个逻辑命名空间(logical namespace),它指向一个或多个分片(shards). 分片(shard) 是 工作单元(work ...