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. PhoneGap和Cordova应该用哪一个?

    就目前来看,cordova是一个移动应用开发框架,你基于这个东西可以用网页代码作出APP.Phonegap Build是一个在线打包工具,你把使用cordova写好的项目给Phonegap Build ...

  2. apm固定翼调试方法

    APM飞控传说是大神的神器新手的噩梦,APM是个便宜又好用的飞控~刚开始给我的天行者X5按APM飞控的时候也查询搜索了很多,参数值,修改和混控和混控量的修改翻遍了资料发现咱们论坛教程比较少,所以开帖总 ...

  3. 在状态栏增加图标(Android 6.0)

    在Android 启动之后,在SystemServer.java的run方法中,调用了StatusBarManagerService的构造方法,在StatusBarManagerService的构造方 ...

  4. 总结Jquery中获取自定义属性使用.attr()和.data()以及.prop()的区别

    一..attr()和.data()的区别: .attr()和.data()本质上属于DOM属性和Jquery对象属性的区别. 看一个例子: <!DOCTYPE html> <html ...

  5. 把玩Fedora29操作系统

    reference:   https://ask.csdn.net/questions/657608 1.下载Fedora服务器版操作系统 https://getfedora.org/zh_CN/se ...

  6. L1-060 心理阴影面积

    这是一幅心理阴影面积图.我们都以为自己可以匀速前进(图中蓝色直线),而拖延症晚期的我们往往执行的是最后时刻的疯狂赶工(图中的红色折线).由红.蓝线围出的面积,就是我们在做作业时的心理阴影面积. 现给出 ...

  7. Python机器学习(python简介篇)

    1.Python 数据类型 Python 内置的常用数据类型共有6中: 数字(Number).布尔值(Boolean).字符串(String).元组(Tuple).列表(List).字典(Dictio ...

  8. FCC-js算法题解题笔记

    题目链接:https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm ...

  9. electron-vue初始桌面应用

    1.安装vue-cli脚手架 npm install -g vue-cli 2.创建项目:vue init simulatedgreg/electron-vue my-project 3.安装依赖 : ...

  10. python爬虫基础_scrapy

    其实scrapy想要玩得好,还是需要大量全栈知识的.scrapy 被比喻为爬虫里的django,框架和django类似. 安装: Linux/mac - pip3 install scrapy Win ...