贴题目

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. [leetcode-485-Max Consecutive Ones]

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  2. 【LeetCode】136. Single Number

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  3. Vulkan Tutorial 25 Images

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 到目前为止,几何图形使用每个顶点颜色进行着色处理,这是一个 ...

  4. 关于SpringMVC中text/plain的编码导致的乱码问题解决方法

    有老铁的项目出现个问题,就是用SpringMVC给前台返回一句话,是String类型的,然后前台接收到是乱码. 然后以为是简单的response的编码问题,就在方法体中开始给response设置编码, ...

  5. (转载)oracle 在一个存储过程中调用另一个返回游标的存储过程

    原文链接:http://www.jb51.net/article/20160.htm 实际项目当中经常需要在一个存储过程中调用另一个存储过程返回的游标,本文列举了两种情况讲述具体的操作方法. 第一种情 ...

  6. Android之圆点导航的两个案例(ViewPager)

    案例一效果: 布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  7. Android补间动画笔记

    布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  8. c++调用python系列(1): 结构体作为入参及返回结构体

    最近在打算用python作测试用例以便对游戏服务器进行功能测试以及压力测试; 因为服务器是用c++写的,采用的TCP协议,当前的架构是打算用python构造结构体,传送给c++层进行socket发送给 ...

  9. hdu_3336: Count the string(KMP dp)

    题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...

  10. nyoj_3:多边形重心问题(计算几何)

    基础的计算几何 多边形的n个顶点按*时针方向给出 由任意n边形可分解为n-2个三角形,各三角形面积面积与重心易得,故有各三角形的面积及重心 用重心公式可求得多边形的面积与重心 题目链接: http:/ ...