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,也就是说要耗一定时.输出方案. 解 ...
随机推荐
- 开源HUSTOJ
hustoj -- 请一定认真看完本页再动手安装,以免无谓的折腾!====== 根据你选择的发行版不同,从下面三个脚本里选一个来用. <b>不要相信百度来的长篇大论的所谓教程,那些都是好几 ...
- nginx 日志打印post请求参数
在日志格式后面加上 $request_body 配置信息 log_format main '$remote_addr - $remote_user [$time_local] "$reque ...
- asp.net IHttpHandler浅析
在asp.net程序中,我们可以通过配置url的path路径的方式,将某个path路径下的请求交给指定的IHttpHandler去处理,这便是对request请求进行编程. 一.新建一个framewo ...
- vue+uwsgi+nginx部署路飞学城
vue+uwsgi+nginx部署路飞学城 有一天,老男孩的苑日天给我发来了两个神秘代码,听说是和mjj的结晶 超哥将这两个代码,放到了一个网站上,大家可以自行下载 路飞学城django代码 ht ...
- Linux VPS自动定时备份网站文件和MYSQL数据库到FTP空间(LNMP)
如果我们网站更新不是很频繁,我们可以定期手动进行备份网站文件和MYSQL数据库导出.如果我们网站数据更新频繁,且数据尤为重要,建议要采用定期自动 备份,至少需要多备份数据,无论我们选择何种优秀的VPS ...
- unity解压缩zip发布后的一些问题
前段时间项目需要,搞了下zip的解压缩问题,也是利用ICSharpCode.SharpZipLib.dll来处理的zip,这里说下之前遇到的坑(这里提供我用的这个库ICSharpCode.SharpZ ...
- DAY1 练习
要求:⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化), 如果三次用完了之后 问是否再试试 再给三次机会 如果不想试了说没有机会了. list = [{'usernam ...
- JavaScript “函数重载”
函数重载(function overloading)必须依赖两件事情:判断传入参数数量的能力和判断传入参数类型的能力. JavaScript的每个函数都带有一个仅在这个函数范围内作用的变量argume ...
- Python之列表方法
def __init__(self, seq=()): """ list() -> new empty list list(iterable) -> new ...
- maven profile实现多环境配置
每次项目部署上线都需要手动去修改配置文件(比如数据库配置,或者一个自定义的配置)然后才能打包,很麻烦,网上找到 maven profile可以完成这个工作,记录如下: 环境:eclipse + spr ...