Problem Description
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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int q[22];
int vis[22];
int cmp(int a,int b)
{
return a<b;
}
int t,n,s;
void dfs(int num,int k,int length) //num 是已完毕的边 k是 数组的位置 length是当前边的长度
{
if(t==1)
return ;
if(num==4)
{
t=1;
return ;
}
if(length==s)
{
dfs(num+1,0,0);
if(t==1)
return ;
}
for(int i=k; i<n; i++)
{
if(length+q[i]<=s&&vis[i]==0)
{
vis[i]=1;
dfs(num,i+1,length+q[i]);
vis[i]=0;
}
}
}
int main()
{
int m;
scanf("%d",&m);
while(m--)
{
t=0;
s=0;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&q[i]);
s=s+q[i];
}
if(s%4!=0)
{
printf("no\n");
continue;
}
sort(q,q+n,cmp);
s=s/4;
if(q[n-1]>s)
{
printf("no\n");
continue;
}
dfs(0,0,0);
if(t==1)
printf("yes\n");
else
printf("no\n"); }
return 0;
}

标准的回溯  可惜 我没有看到这个问题 要运行数据

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 1518 Square 搜索的更多相关文章

  1. hdu 1518 Square(深搜+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...

  2. hdu 1518 Square 深搜,,,,花样剪枝啊!!!

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  3. hdu 1518 Square 木棍建正方形【DFS】

    题目链接 题目大意: 题意就是输入棍子的数量和每根棍子的长度,看能不能拼成正方形. #include <bits/stdc++.h> using namespace std; int n, ...

  4. HDU 1518 Square(DFS)

    Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...

  5. HDU 1518 Square

    解题思路:sum%4!=0    , max<sum/4 #include<iostream>#include<cstdio>#include<cstring> ...

  6. Square HDU 1518 搜索

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

  7. 杭电1518 Square(构成正方形) 搜索

    HDOJ1518 Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. hdu 1518 拼正方形

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

  9. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

随机推荐

  1. Android 网络通信框架Volley基本介绍

    Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...

  2. Creating Contextual Menus创建上下文菜单

    A contextual menu offers actions that affect a specific item or context frame in the UI. You can pro ...

  3. SWT中的GridLayout(转)例子不错

    GridLayout 是一个非常强大的布局管理器,它可以实现很多复杂的布局,名字中暗示它将所有控件放置在类似网格的布局中.^__^GridLayout 有两个构造函数. GridLayout的构造函数 ...

  4. Java--调试--单步调试,断言,单元测试

    单步调试:主要查看变量内容的变化    1.设置断点位置,设置在可能出现问题的代码 2.以Debug as方式运行 程序 3.F5 --> step into 进入方法内部进行调试    F6 ...

  5. 行人检测(Pedestrian Detection)资源整合

    一.纸 评论文章分类: [1] D. Geronimo, and A. M.Lopez. Vision-based Pedestrian Protection Systems for Intellig ...

  6. 设计模式(一)工厂模式Factory(创建类型)

    设计模式一 工厂模式Factory 在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.可是在一些情况下, new操作符直接生成对象会带来一些问题. ...

  7. 点击 下载 pdf

     <iframe id="fileDownFrame" src="" style="display:none; visibility:hidde ...

  8. JAVA技术交流群

    推荐:组[八方扑灭]:http://jq.qq.com/?_wv=1027&k=RFLXu0. QQ: 292352612  集团专注于技术.软件project.JAVA.c\c++.WEB. ...

  9. Install Oracle 11gR2 on Debian wheezy(转)

    Install Oracle 11gR2 on Debian wheezy 出处:http://gaiustech.wordpress.com/2013/06/26/howto-install-ora ...

  10. HDU 4284 状压dp+spfa

    题意: 给定n个点 m条无向边 d元. 以下m行表示每条边 u<=>v 以及花费 w 以下top 以下top行 num c d 表示点标为num的城市 工资为c 健康证价格为d 目标是经过 ...