贴题目

Square
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 24604   Accepted: 8449

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".
 
一道和1011很像的剪枝,在做了1011之后我写了这道题,结果不幸的wa了。所以说,1011的测点很水QAQ
读题后发现以下剪枝:
1、如果所有木棍和加起来不能被4整除,那么不能构成square;
2、将所有木棍和除以4的值(边长)保存,如果子段中的最大长度大于这一值,那么不能构成square;
3、与1011同理,同一长度的木棍不需重复判断;
4、将木棍从大到小开始找,因为越小的木棍灵活度越高。
分析可知,如果已经构成了3条边符合要求,那么一定能构成一个正方形。所以在进行第四边查找时,我们可以直接确定可以构成正方形。
代码如下:
#include<cstdio>
#include<algorithm>
#include<cstring>
bool flag,zt[];
int n,m,a[],sum,l,mm,i;
using namespace std;
bool cmp(const int a,const int b){
return a>b;
}
void dfs(int num,int len,int now){
if(flag)return ;
if(num==){
flag=true;
return;
}
if(len==sum){
dfs(num+,,);
if(flag)return ;
}
int i;
for(i = now; i <= mm ; ++i){
if(!zt[i]&&len+a[i]<=sum){
zt[i]=true;
dfs(num,len+a[i],i+);
zt[i]=false;
if(flag)return ;
}
}
return ;
}
void start(){
sum=;
memset(a,,sizeof(a));
flag=false;
i=;
}
int main(){
scanf("%d",&n);
while(n--){
start();
scanf("%d",&m);
mm=m;
while(m--){
++i;
scanf("%d",&a[i]),sum+=a[i];
}
sort(a+,a+mm+,cmp);
if(sum%||a[]>sum){
printf("no\n");
continue;
}
sum/=;
memset(zt,,sizeof(zt));
dfs(,,);
if(flag)printf("yes\n");
else printf("no\n");
}
return ;
}
dfs的习题重要的就是剪枝,拿到题目一定要多分析,尽可能多地找出不必要的枝,来提高时间效率。博主自己还要注意的就是细心,总是出一些没有必要的小毛病,希望能改掉这个毛病。
今天写的比较匆忙,不足的地方麻烦大家帮我提出来了~嗯,以后还是要加强练习。

dfs+剪枝:poj2362的更多相关文章

  1. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  3. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  4. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  5. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  6. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

  9. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  10. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

随机推荐

  1. java基础回顾(五)线程详解以及synchronized关键字

    本文将从线程的使用方式.源码.synchronized关键字的使用方式和陷阱以及一些例子展开java线程和synchronized关键字的内容. 一.线程的概念 线程就是程序中单独顺序的流控制.线程本 ...

  2. [leetcode-474-Ones and Zeroes]

    In the computer world, use restricted resource you have to generate maximum benefit is what we alway ...

  3. 【Android Developers Training】 29. 从Activity获得结果

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  4. CSS input type="number"出现上下箭头时解决方案

    input type="number"时录入内容不可控制,解决方案是在css中添加//火狐input[type=number] {      -moz-appearance:tex ...

  5. AppDelegate减负之常用三方封装 - 友盟推送篇

    之前分享过集成友盟推送的方法, 需要的朋友可以查看一下链接: http://www.cnblogs.com/zhouxihi/p/6533058.html 一般开发中我们比较多使用的三方有友盟推送, ...

  6. Python3.5学习笔记-文件操作

    在Python中,操作文件对象使用open函数来创建,下表列出了常用的操作file的函数: 序号 方法及描述 1.file.close() 关闭文件.关闭后文件不能再进行读写操作. 2.file.fl ...

  7. accp8.0转换教材第4章MySQL高级查询(二)理解与练习

    知识点:EXISTS子查询.NOT EXISTS子查询.分页查询.UNION联合查询 一.单词部分 ①exist存在②temp临时的③district区域 ④content内容⑤temporary暂时 ...

  8. ReOut

    package JBJADV003;import java.io.*;public class ReOut { /** * @param args */ public static void main ...

  9. Unity-Shader-镜面高光Phong&BlinnPhong-油腻的师姐在哪里

    [旧博客转移 - 2016年4月4日 13:13 ] 油腻的师姐: 以前玩过一款很火热的端游<剑灵>,剑灵刚出来的时候,某网页游戏广告视频中有句台词:"我不断的在寻找,有你的世界 ...

  10. Servlet中 End event threw exception,错误404 的解决方法

    End event threw exception    SEVERE: End event threw exceptionjava.lang.reflect.InvocationTargetExce ...