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. ASP.NET MVC学习系列 WebAPI初探

    转自http://www.cnblogs.com/babycool/p/3922738.html 一.无参数Get请求 一般的get请求我们可以使用jquery提供的$.get() 或者$.ajax( ...

  2. c# 获取MAC IP TCP列表

    转载自baidu:http://hi.baidu.com/jackeyrain/item/ff94efcfd5cf3a3099b498e9 namespace Public { public clas ...

  3. phpMyAdmin教程 之 创建新用户/导入/导出数据库

    盗用了被人的教程. 需要看就点击进去吧.复制过来实在是过意不去 http://www.wpdaxue.com/phpmyadmin-import-export-database.html

  4. Android - IOExceptionConnection to xxx refused.

    还是stackoverflow上老外牛,往google上type一下,就找到原因了. 今天在使用Apache提供的HttpClient连接Tomcat服务器,使用log捕获异常的时候,提示说:IOEx ...

  5. 'EntityValidationErrors' property for more details

    很多小猿遇到这个Exception 的时候,都会有点无厘头.这个时候最好try-- catch下,找到出错的地方.本人习惯在页面上加个lable标签,把exc msg(exception messag ...

  6. 邓白氏编码(duns number)申请入口的路径-苹果开发者申请必

    http://tieba.baidu.com/p/3861287522 这个网址有详细的介绍

  7. 配置wamp开发环境【1】

    新手在PHP网站建设时,会使用使用PHP的集成开发环境,这样利于开发和理解!但是做为一个网站开发人员,会独立的配置开发环境这是必须的……因为集成的环境毕竟是固定的,不利于自己的开发.好,废话少说咱现在 ...

  8. MFC error C2065: “IDD_DIALOG1” : 未声明的标识符 转载

    error C2065: “IDD_DIALOG1” : 未声明的标识符 1.编译时提示error C2065: “IDD_DIALOG1” : 未声明的标识符 2.错误的可能原因及解决方法如下: 原 ...

  9. diff函数的实现——LCS的变种问题

    昨天去去哪儿笔试,碰到了一个我们一直很熟悉的命令(diff——ubuntu下面),可以比较字符串,即根据最长公共子串问题,如果A中有B中没有的字符输出形式如下(-ch),如果A中没有,B中有可以输出如 ...

  10. ueditor之ruby on rails 版

    最近公司的项目开始要使用ueditor了,但是ueditor却没有提供rails的版本,因此需要自己去定制化ueditor来满足项目的需求.不多说了,先简要说明下使用方法: ueditor目录下: 注 ...