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,也就是说要耗一定时.输出方案. 解 ...
随机推荐
- XAMPP本地服务器打不开解决方案
第一步:先开启相关服务:如图 第二步:在浏览器上输入localhost:端口号,(或127.0.0.1:端口号),按回车,就成功登陆本地服务器. =========================== ...
- 【webpack学习笔记】a02-管理资源
在webpack 中,各种资源要引入,要用到module配置,比如css/图片/字体等等. 例如: module.exports = { entry: './src/app.js', //这是入口文件 ...
- EFCore Owned Entity Types,彩蛋乎?鸡肋乎?之鸡肋篇
鸡肋 鸡肋(Chicken ribs),现代汉语词语,出自<三国志·魏书·武帝纪>裴松之注引<九州春秋>曰:"夫鸡肋,弃之如可惜,食之无所得,以比汉中,知王欲还也.& ...
- Java Spring 在线程中或其他位置获取 ApplicationContext 或 ServiceBean
部分一转载自:http://blog.csdn.net/yang123111/article/details/32099329 via @yang123111 部分二转载自:http://www.cn ...
- centos7 无界面静默安装 oracle
环境准备 Centos7.3.64 64位 这里使用的是阿里云 ECS主机(1核,2G内存,40G硬盘) Oracle 11g R2 64位安装介质(版本11.2.0.1)下载地址:http:/ ...
- solrcloud jsonfacet分组聚合 unique计数不准确
jsonfacet分组聚合查询 unique.hll函数问题: 对不同的值进行估算,并非准确的值, 优点:节省内存消耗,用分组算法对不同的值count进行估算 缺点:无法准确统计count(disti ...
- 常忽略的css技巧
1.利用 CSS 的伪类中的content属性获取attr中的信息 效果图:鼠标放上去出现提示 css代码: .box{position:relative;display:inline-block;m ...
- commons-lang常用方法
跟java.lang这个包的作用类似,Commons Lang这一组API也是提供一些基础的.通用的操作和处理,如自动生成toString()的结果.自动实现hashCode()和equals()方法 ...
- Rabbit 集群部署
1.RabbitMQ是用erlang语言编写的,所以我们先安装erlang语言环境 配置erlang语言环境 # vim /etc/yum.repos.d/rabbitmq-erlang.repo [ ...
- Shell bash 数学运算 bc
1.bc命令可以完成浮点数的运算.其中 scale可以指定保留的小数点位数. 2.举例 例1: 例2: