hdu 1518 BFS
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的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- hdu 1518 拼正方形
本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题意:输入几个长度,判断能否拼成正方形. 以下部分参考了网友代码,终于ac啦. #include ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
随机推荐
- Bootstrap 完全教程笔记
Bootstrap CSS Bootstrap 排版 引导主体副本 为了给段落添加强调文本,则可以添加 class=”lead”,这将得到更大更粗.行高更高的文本,如下面实例所示: <h2> ...
- python字典和条件控制
元组tuple 元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表 注意单个tuple元素的时候,元素后面要加",",否则python解析器不会识 ...
- elk的一些零碎知识
1.elasticsearch检查是否健康 http://12.20.511.141:9200/_cat/health?v 绿色表示一切正常, 黄色表示所有的数据可用但是部分副本还没有分配,红色表示部 ...
- Vue的自定义组件之间的数据传递
一,父级传向子级 1,在子级的属性中添加props:['myname',......],参数可以传多个,看具体而定: 2,在父级data中定义好需要传递的变量数据,例如name:"rose& ...
- 记初学python的一些心得
人生苦短,我用python! 其实我自学python也很长一段时间了,但总是去更换学习资料,搞的现在学的不是很好,因为没更换次资料都要从头开始学起,那么分享下我的学习战况吧,不是很好,还将就的能看. ...
- python 语法
Python基础语法举例# 1 缩进和空格表示代码块# 2()连接多行print("()连接多行")str = ("uhfjfjkfj" " ...
- vim学习纪要
普通模式 根据屏幕行上下移动. gj gk g0 g^ g$ 移动到行首第一个非空字符 ^ 反向移动到上一单词的词尾 ge 插入模式 粘贴寄存器中内容 <C-r> 可视模式 移动光标的起始 ...
- node.js安装使用express框架
官网:http://www.expressjs.com.cn/ 使用方式(如果后面需要添加路由等推荐第二种方式,不然需要需要手动添加):一.自己动手搭建 1. 新建项目文件夹,如test,并在命令行中 ...
- 简单理解 SVM
SVM,中文名叫支持向量机. 在深度学习出现以前,它是数据挖掘的宠儿: SVM具有十分完整的数据理论证明,但同时理论也相当复杂. 初识SVM 同其他分类算法一样,SVM分类也是寻找合适的决策边界,为 ...
- C#中委托的同步和异步有什么区别
通过定义委托,来实现同步和异步,委托通过Action和Func来实现,记录学习委托的同步和异步的过程 委托的同步方法 委托的Invoke方法用来进行同步调用. static void Main(str ...