Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9588    Accepted Submission(s): 3127

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
 
開始就理解错题意了
o(╯□╰)o 为什么我总是理解错题意
题目的意思是全部的木棍能否组成一个正方形,而我觉得全部木棍中的一部分能否够构成一个正方形。。

一直都是TLE,,我是枚举了全部的正方形可能的长度,然后进行深搜。

。。

后来看了别人的代码才返现是自己理解错了。
即使题目意思明确了。我还是TLE一次。。原因是我反复搜索了。。
代码:

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std ; int d[30] , m , sum = 0;
bool visited[30] ;
bool DFS(int len , int c ,int pos)
{
if(c==4)
{
return true ;
}
if(sum == len)
{
if(DFS(0,c+1,0))
{
return true ;
}
}
else
{
for(int i = pos ; i < m ; ++i)
{
if(!visited[i])
{
if(len+d[i]>sum)
{
return false;
}
visited[i] = true ;
if(DFS(len+d[i],c,i+1))
{
return true ;
}
visited[i] = false ;
}
}
}
return false ;
} int main()
{
int n ;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
sum = 0 ;
for(int i = 0 ; i < m ; ++i)
{
scanf("%d",&d[i]) ;
sum += d[i] ;
}
if(m<4 || sum%4!=0)
{
puts("no") ;
}
else
{
sort(d,d+m) ;
sum /= 4 ;
if(sum<d[m-1])
{
puts("no") ;
continue ;
}
memset(visited,false,sizeof(visited)) ;
if( DFS(0,0,0) )
{
puts("yes") ;
}
else
{
puts("no") ;
}
}
}
return 0 ;
}

hdu 1518 Square 深搜,,,,花样剪枝啊!!!的更多相关文章

  1. 【笔记】「pj复习」深搜——简单剪枝

    深搜--简单剪枝 说在最前面: 因为马上要 NOIP2020 了,所以菜鸡开始了复习qwq. pj 组 T1 ,T2 肯定要拿到满分的,然后 T3 , T4 拿部分分, T3 拿部分分最常见的做法就是 ...

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

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

  3. hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. 【深搜加剪枝】【HDU1455】【Sticks】

    题目大意:有一堆木棍 由几个相同长的木棍截出来的,求那几个相同长的木棍最短能有多短? 深搜+剪枝 具体看代码 #include <cstdio> #include <cstdlib& ...

  5. Block Breaker HDU - 6699(深搜,水,写下涨涨记性)

    Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...

  6. 一本通例题埃及分数—题解&&深搜的剪枝技巧总结

    一.简述: 众所周知,深搜(深度优先搜索)的时间复杂度在不加任何优化的情况下是非常慢的,一般都是指数级别的时间复杂度,在题目严格的时间限制下难以通过.所以大多数搜索算法都需要优化.形象地看,搜索的优化 ...

  7. 深搜+DP剪枝 codevs 1047 邮票面值设计

    codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description ...

  8. HDOJ/HDU 1015 Safecracker(深搜)

    Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...

  9. HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

随机推荐

  1. BZOJ 4942 NOI2017 整数 (压位+线段树)

    题目大意:让你维护一个数x(x位数<=3*1e7),要支持加/减a*2^b,以及查询x的第i位在二进制下是0还是1 作为一道noi的题,非常考验写代码综合能力,敲+调+借鉴神犇的代码 3个多小时 ...

  2. [CodeForces]529B Group Photo 2

    AK爷GhostCai的电脑又蓝屏了Orz 贪心题,确定一个maxh,限定h不大于一个值.枚举maxh. check的时候的细节很多: 1.h>maxh但w<maxh交换的时候需要占用交换 ...

  3. ThinkPHP 跨模块调用操作方法(A方法与R方法)

    ThinkPHP 跨模块调用操作方法(A方法与R方法) 跨模块调用操作方法 前面说了可以使用 $this 来调用当前模块内的方法,但实际情况中还经常会在当前模块调用其他模块的方法.ThinkPHP 内 ...

  4. Linux 网络搭建

    如果系统环境崩溃.   调用/usr/bin/vim /etc/profile Windows    1 本地连接使用固定IP   vmware 8    2  修改Windows的hosts地址   ...

  5. Java ThreadLocal类学习

    ThreadLocal为线程局部变量,通过线程名(key)-对象(value)的Map来获取每个线程对应的对象.我们不能通过ThreadLocal处理多线程并发问题,但是每个线程可以通过ThreadL ...

  6. [Angular] Set Metadata in HTTP Headers with Angular HttpHeaders

    Besides sending (or requesting) the actual data to the server API, there’s also often the need to se ...

  7. m_Orchestrate learning system---十二、为什么thinkphp验证场景里面的多个属性之间是逗号

    m_Orchestrate learning system---十二.为什么thinkphp验证场景里面的多个属性之间是逗号 一.总结 一句话总结:因为是数组啊 1 protected $scene ...

  8. 去除iframe滚动条

    主页面的IFRAME中添加:scrolling="yes" 子页面程序代码: 让竖条消失: <body style='overflow:scroll;overflow-x:a ...

  9. zzulioj--1712--神秘的数列(水题)

    1712: 神密的数列 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 92 SubmitStatusWeb Board De ...

  10. js小结2

    1.includes和contains 对于字符串,数组来说,判断包含是includes,对classList是contains 2.编辑span内容,enter提交(如何避免keydown之后换行: ...