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. SQL Server 基础:Join用法

    Join(麻蛋  废话不多说 有图有真相) 测试数据脚本 CREATE TABLE Atable ( S# INT, Sname ), Sage INT, Sfrom ) ) insert into ...

  2. 大话RAC介质恢复---联机日志损坏

    对联机日志的损坏要根据日志状态进行分析,联机日志一般会有Current.Active和Inactive三种状态.Inactive状态不会造成数据丢失.而Active和Current状态的日志一般会造成 ...

  3. SQL基础篇---函数及其函数配套使用的关键字

    一.数值函数 知识点1 SUM 求总和 SELECT breakfast,sum(price) FROM my_foods GROUP BY breakfast ORDER BY SUM(price) ...

  4. hdu 3530 Subsequence

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3530 Subsequence Description There is a sequence of i ...

  5. Tutorial: Facebook analytics using Power BI Desktop

    In this tutorial you learn how to import and visualize data from Facebook. During the tutorial you'l ...

  6. OpenStack:初识

    OpenStack提纲:-------------------------------------------初识OpenStack, 千头万绪, 不知所措. 逐渐剥茧抽丝, 厘清思路...一. Op ...

  7. SQLite之写一个表

    1.首先你需要一个路径. 获取document目录并返回数据库目录 - (NSString *)dataFilePath{ NSArray *paths = NSSearchPathForDirect ...

  8. find用法

    find - 递归地在层次目录中处理文件 总 find [path...] [expression] 描 这个文档是GNU版本 find 命令的使用手册. find 搜索目录树上的每一个文件名,它从左 ...

  9. iTween基础之Fade(淡入淡出)

    一.基础介绍:二.基础属性 原文地址: http://blog.csdn.net/dingkun520wy/article/details/50923665 一.基础介绍 FadeTo:从当前透明度变 ...

  10. 如何查看系统内置的shell命令及帮助信息

    1.打开终端,输入help命令可以查看系统默认的shell(通常是bash)的内置的shell命令列表: [tansheng@localhost ~]$ help GNU bash, version ...