Square

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

Total Submission(s): 11099 Accepted Submission(s): 3566

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

题意就是:看这个数组中的数字组合是否能够构成一个正方形

不能分割数字,不能重复组合

代码:

#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,s,su,a[1010],vis[1010],len;
bool cmp(int a,int b)
{
return a>b;//从大到小排序
}
void dfs(int a1,int a2,int a3)//(0,0,1)
{
if(a1==3)/**只需要筹齐3次,那么剩下的一定能够成len长度**/
{
su=1;
return ;
}
if(su==1)
return ;/**优化时间**/
for(int i=a3; i<=n; i++)
{
/**对于那些用过的和不符合条件的,for那里可以不扫,故从a3开始**/
/**a3前面的对于最初的a2来说一定不符合**/
if(vis[i]==0)
{
vis[i]=1;
if(a2+a[i]==len)
{
dfs(a1+1,0,1);
/**但是换另外一条边的时候a3要改回1,因为那些未用的,对上一条边来说不符合条件的,可能符合这条边的条件**/
}
else if(a2+a[i]<len)
{
dfs(a1,a2+a[i],i+1);
/**没筹齐从i+1继续,前面的不符合**/
while(a[i]==a[i+1])
i++;
//回溯后如果后面的相同那么不需要再DFS
//前面的数和后面的相同就可以跳过这个数,剪枝
}
vis[i]=0;
}
}
} int main()
{
int i;
scanf("%d",&s);
while(s--)
{
su=0;
len=0;
memset(vis,0,sizeof(vis));
//memset函数在string.h头文件中
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf("%d",&a[i]);
len=len+a[i];
}
int mm=len/4;
sort(a+1,a+n+1,cmp);
//在algorithm头文件中
if(len%4==0&&a[1]<=mm&&n>=4)
{
len/=4;
dfs(0,0,1);
if(su==1)
printf("yes\n");
else
printf("no\n");
}
else
{
printf("no\n");
}
}
return 0;
}

HDOJ1518Square 深搜的更多相关文章

  1. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  2. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  3. 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

  4. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  5. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  6. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

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

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

  8. 【wikioi】1049 棋盘染色(迭代深搜)

    http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...

  9. poj1190 生日蛋糕(深搜+剪枝)

    题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...

随机推荐

  1. 微信热修复tinker及tinker server快速接入

    博客: 安卓之家 掘金: jp1017 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 当前热修复方案很多,今天研究了下微信的tinker,使用效果还是不错的,配合tinker serv ...

  2. 研究WCF并发及处理能力的控制

    WCF 跟并发 性能相关的几个配置:1.系统控制的客户端网络连接并发(如果服务端也需要并发请求的话这个参数也是需要的):          <configuration>          ...

  3. oracle set命令

    SQL>set colsep' ';     //-域输出分隔符SQL>set echo off;     //显示start启动的脚本中的每个sql命令,缺省为onSQL> set ...

  4. 分享red hat linux 6上安装oracle11g时遇到的gcc: error trying to exec 'cc1': execvp: No such file or directory的问题处理过程

    安装环境:Red Hat Linux 6.5_x64.oracle11g 64bit 报错详情: 安装到68%时弹窗报错: 调用makefile '/test/app/Administrators/p ...

  5. 01-Objective-C

    前言   目 前来说,Objective-C(简称OC)是iOS开发的核心语言,在开发过程中也会配合着使用C语言.C++,OC主要负责UI界面,C语言.C++ 可用于图形处理.近来,流传Ruby.C# ...

  6. [转] 《高性能HTML5》读后整理的Web性能优化内容

    读后感 先说说<高性能HTML5>这本书的读后感吧,个人觉得这本书前两章跟书的标题完全搭不上关系,或者说只能算是讲解了“高性能”这三个字,HTML5完全不见踪影.个人觉得作者应该首先把HT ...

  7. ES5中数组新增的方法说明

    一.前言-索引 ES5中新增的不少东西,了解之对我们写JavaScript会有不少帮助,比如数组这块,我们可能就不需要去有板有眼地for循环了. ES5中新增了写数组方法,如forEach (js v ...

  8. js 中的流程控制—while和do while

    while语句: while(exp){  }如果为true ,执行代码块里的语句,如果为false,跳出循环 <script> var i =1 ; while (i<10){ / ...

  9. 解决SDK Manager无法更新问题

    因为google被封了,导致Android SDK Manager无法更新,解决方案如下: 1.选择tools->options,跳出Settings页面 2.设置HTTP Proxy代理,设置 ...

  10. 使用Autofac部署IIS6.0时未能加载文件或程序集“System.Core, Version=2.0.5.0...“

    错误信息 .net4.0项目中使用autofac这个IOC容器,在部署在win2003+iis6时出现以下错误. “/”应用程序中的服务器错误. --------------------------- ...