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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std;
int a[25];
int sum,n,s;
bool mark[25];
bool cmp(int a,int b) {
return a>b;
}
int dfs(int step,int cur,int index) {
if(step==3) return 1;//ye zi
for(int i=index;i<n;i++){
if(!mark[i]) {
if(cur+a[i]==s) {
mark[i]=true;
if(dfs(step+1,0,0)) return 1;
mark[i]=false;
}
else if(cur+a[i]<s){
mark[i]=true;
if(dfs(step,cur+a[i],i+1)) return 1;
mark[i]=false;
}
}
}
return 0;
}
int main(){
// freopen("INPut.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--) {
memset(mark,false,sizeof(mark));
sum=0;
scanf("%d",&n);
int M=0;
for(int i=0;i<n;i++) {
scanf("%d",&a[i]);
sum+=a[i];
M=max(M,a[i]);
}
s=sum/4;
if(sum%4||M>s) {
printf("no\n");
continue;
} sort(a,a+n,cmp);
if(dfs(0,0,0)) {
printf("yes\n");
}
else printf("no\n");
}
return 0;
}

hdu 1518 BFS的更多相关文章

  1. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  2. Square HDU 1518 搜索

    Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...

  3. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  4. hdu 1518 拼正方形

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题意:输入几个长度,判断能否拼成正方形. 以下部分参考了网友代码,终于ac啦. #include ...

  5. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  6. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  7. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  8. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

  9. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

随机推荐

  1. day44-Celery异步分布式

    celery异步分布式Celery是一个python开发的异步分布式任务调度模块.Celery本身并不提供消息服务,使用第三方服务,也就是borker来传递任务,目前支持rebbimq,redis, ...

  2. "No entity found for query"(转)

    很久以前一直用EntityManager下getResultList()查询数据,即便一个数据亦是如此,所以要频繁的List.get(0).新接口getSingleResult()出现了,然后就开始频 ...

  3. 使用该方法在ubuntu下安装flashplayer的rpm包

    Ubuntu的软件包格式是deb,如果要安装rpm的包,则要先用alien把rpm转换成deb. sudo apt-get install alien #alien默认没有安装,所以首先要安装它 su ...

  4. 输入系统:进程间双向通信(socketpair+binder)

    一.双向通信(socketpair) socketpair()函数用于创建一对无名的.相互连接的套接子,如果函数成功,则返回0,创建好的套接字分别是sv[0]和sv[1]:否则返回-1,错误码保存于e ...

  5. 使用 navicat 导入导出数据库

    1.使用 navicat 导出数据库 2.使用 navicat 导入数据库导入之前需要先建好数据库 3.可以直接使用navicat 到数据传输功能直接将一个数库copy到另一个数据库

  6. 使用IdentityServer4,在一个ASPNetCore项目中,配置oidc和api的AccessToken两种认证授权

    1.配置两种认证方式 JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication(op ...

  7. Vue语法学习第一课——插值

    学习关于Vue的插值语法 ① 文本值 : "Mustache"语法,即双大括号 <span>Message:{{msg}}</span> 注:双大括号中的m ...

  8. opencv3.1+cmake3.7.2+cuda9.1+vs2015+opencv-contrib+win10x64

    下载cuda https://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&targ ...

  9. Beta冲刺 3

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10116813.html 作业博客:https://edu.cnblogs.com/campus ...

  10. jmeter 常用组件详解

    1.测试计划(test plan) 描述一个性能测试,包含本次测试所有相关功能 2.threads(users)线程 Setup thread group: 一种特殊类型的线程,可用于执行预测试操作. ...