Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5443    Accepted Submission(s): 1732

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
 

Source
 

Recommend
LL
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int sum;
int n;
int a[25];
int vis[25];
bool dfs(int cur,int time,int k)
{
    if(time==3)
    {
       return true;
    }
    for(int i=k-1;i>=0;i--)
    {
        if(!vis)
        {
            vis=1;
            if(cur+a==sum)
            {
                if(dfs(0,time+1,n))
                    return true;
            }
            else if(cur+a<sum)
            {
                if(dfs(cur+a,time,i)) return true;
            }
            vis=0;
        }
    }
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
while(T--)
{
    memset(vis,0,sizeof(vis));
    sum=0;
    int maxn=-1;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a);
        sum+=a;
        maxn=max(maxn,a);
    }
    if(sum%4!=0||maxn>sum/4)
    {
        puts("no");
        continue;
    }
    sum=sum/4;
    sort(a,a+n);
    if(dfs(0,0,n)) puts("yes");
    else puts("no");
}
    return 0;
}

HDOJ 1518 Square的更多相关文章

  1. hdoj 1518 Square 【dfs】

    题意:给出n个(不同长度的)棍子,问能不能将他们构成一个正方形. 策略:深搜. hdoj 1455的简化版 代码: #include <stdio.h> #include <stri ...

  2. 【HDOJ】1518 Square

    DFS+剪枝.与HDOJ 1455如出一辙. #include <stdio.h> #include <stdlib.h> #include <string.h> ...

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

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

  4. HDOJ 1398 Square Coins 母函数

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. HDU 1518 Square 搜索

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

  6. HDU 1518 Square(DFS)

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

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

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

  8. HDOJ 1398 Square Coins

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

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

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

随机推荐

  1. lua进阶(二)

    第五章 函数 函数有两种用途:1.完成指定的任务,这种情况下函数作为调用语句使用:2.计算并    返回值,这种情况下函数作为赋值语句的表达式使用. function function_name( . ...

  2. Sql Server 语句

    ##目录 #####清除缓存 DBCC FREEPROCCACHE; DBCC DROPCLEANBUFFERS; SELECT stock.IdStock, stock.Descr FROM [In ...

  3. MYSQL 一些用法

    LOAD DATA LOCAL INFILE 'C:/xampp/htdocs/test/file/sample.csv' INTO TABLE sample1 FIELDS TERMINATED B ...

  4. C++实现的哈希搜索

    C++实现的哈希搜索 程序内容 Complete a text searching engine using hash table. 完成一个文本搜索引擎,使用哈希表 程序设计 程序流程图 程序代码 ...

  5. [rsync+inotify]——监控客户端文件变化,rsync同步到服务器

    关于rsync的配置请参考博文:http://www.cnblogs.com/snsdzjlz320/p/5630695.html 实验环境 (1) Rsync服务器:10.0.10.158 (2) ...

  6. Cocos2dx中利用双向链表实现无限循环滚动层

    [Qboy原创] 在Cocos2dX 3.0 中已经实现一些牛逼的滚动层,但是对于有一些需要实现循环滚动的要求确没有实现,笔者在前段时间的一个做了一个游戏,需求是实现在少有的(13个)英雄中进行循环滚 ...

  7. mysql启动问题access denied for user 'root'@'localhost'(using password:YES)

    安装Mysql后利用SQLyogEnt启动是提示“access denied for user 'root'@'localhost'(using password:YES)”,开始我还为是因为是密码问 ...

  8. java中的substring用法

    String str="我是中国人"; str = str.substring(0, 2) +"_"+str.substring(3, 4); 结果:str=& ...

  9. android 开发 制作弹出等待进度条

    技术点: dialog:ProgressBar:animated-rotate: 弹出框: import com.carspeak.client.R; import android.app.Dialo ...

  10. Surface Pro 3 扩展坞体验

    Surface Pro 3 的确是Microsoft的诚意之作,虽然价格不便宜,但值得入手. 买完Surface Pro 3 后,没事在某网站上闲逛,这货(扩展坞)映入我的眼帘,看完后,心想,这货必须 ...