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. visual studio 2017 使用码云gitee进行源代码管理

    在码云新建项目 复制项目地址 visual studio  操作 新建项目 提交到码云

  2. like

    5.在WHERE中使用like做模糊查询    %符号表示0到多个任意字符    _符号表示1个任意字符     //查询名字中含有O字符的员工信息   select empno,ename   fr ...

  3. web前端project师知识汇总

    分类: Web开发应用  一.何为Web前端project师?           前端project师,也叫Web前端开发project师.他是随着web发展.细分出来的行业.Web前端开发proj ...

  4. IBM AppScan官方帮助文档错别字缺陷,IBM的測试人员也太粗心了吧

    袁术=元素?

  5. Java推断类和实例的关系

       通常我们使用instanceOf关键字来推断一个对象是否是类的实例,近期博主看到isInstance关键字,不解与instanceOf的差别,故度娘了一下,顺便涨了一下姿势.    Java中推 ...

  6. xxx while the managed IDbConnection interface was being used: Login failed for user xxx

    Process cube的时候遇到如下错误.   Errors in the high-level relational engine. The following exception occurre ...

  7. App.config:配置系统未能初始化的异常

    如上图所示:App.config文件是这样配置的,在后台代码”ISchedulerFactory scheduler = new StdSchedulerFactory();“中抛出了异常 经网上查资 ...

  8. 解码URLDecode和编码URLEnCode

    在前台往后台传递参数的时候,在前台进行编码,在后台接收参数的时候,用Decode进行解码: 如果url中包含特殊字符如:&.html标签 <tr><td>等导致url无 ...

  9. Linux部署之批量自动安装系统之Kickstart篇

    1.         安装   2.         在桌面环境下啊配置   3.         Kickstart之基本配置   4.         Kickstart之安装方法   5.    ...

  10. http状态码304

    服务器对客户端返回HTTP/1.1 304  意思是服务端告诉客户端 我的的缓存没有改变你不需要来取了,就用你自己本地的吧! 浏览器的三种缓存协商机制: if-modified-since (基于最后 ...