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. 使用linq获得当前文件夹下的下一级满足条件的文件夹

    使用linq获得当前文件夹下的下一级满足条件的文件夹.             SPFolderCollection subAlbums = Folder.SubFolders;            ...

  2. javascript - 图片的幻灯片效果

    javascript 代码: <script type="text/javascript"> function select_play() { var select_p ...

  3. oracle 过程函数,包的区别和联系

    一.过程与函数区别 1.过程可以有0~N个返回参数,通过OUT or IN OUT参数返回:函数有且仅有1个返回值,通过return语句返回. 2.调用过程时,可做为单独的语句执行:调用函数时,函数必 ...

  4. javascript String 和StringBuffer 的应用

    显示情况时Javascript中并没有StringBuffer类,一种主流的Javascript StringBuffer类的实现是通过prototype构造一个StringBuffer类. Stri ...

  5. iOS截屏代码

    转载自:http://m.open-open.com/m/code/view/1420469506375 1.普通界面 /** *截图功能 */ -(void)screenShot{ UIGraphi ...

  6. 一看就懂ReactJS

    现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领 ...

  7. 24种设计模式--适配器模式【Adapter Pattern】

    今天讲适配器模式,这个模式也很简单,你笔记本上的那个拖在外面的黑盒子就是个适配器,一般你在中国能用,在日本也能用,虽然两个国家的的电源电压不同,中国是 220V,日本是 110V,但是这个适配器能够把 ...

  8. InstallShield 创建自己的Dialog

    1.在"User Interface"-"Dialogs"下,在All Dialogs右击"New Dialogs-"创建自己的Dialog ...

  9. Apache 支持.htaccess

    ******************************************************************************* Apache 服务器 ********* ...

  10. Sharepoint 问题集锦 - 外部列表(external list) - 读取当前用户上下文或用户名作为筛选参数

    在创建外部列表过程中,往往需要添加筛选参数,而较多开发用户,会关心如何在外部列表中,只显示当前用户相关的行.本例子中,我们以任务数据表来做例子,看看如何实现这个需求. 1)数据表tbl_task: t ...