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. ng-summit and $watch() funciton

    <div ng-app> <form ng-submit="requestFunding()" ng-controller="StartUpContro ...

  2. 使用WIF实现单点登录Part IV —— 常见问题

    InvalidOperationException: ID1073: 尝试使用 ProtectedData API 解密 Cookie 时出现 CryptographicException (有关详细 ...

  3. 6.css文本样式

    文本样式,只要针对的是文本的效果和文本的方位,即文本样式和文本控制总结起来有一表中的属性可用: 属性名 说明 CSS 版本 text-decoration 装饰文本出现各种划线 1 text-tran ...

  4. 3.css中的颜色

    css中颜色的设置形式主要有三种方式:颜色名称.十六进制代码和十进制代码. 在古老的 HTML4 时,颜色名称只有 16 种. 颜色名称 十六进制代码 十进制代码 含义  black  #000000 ...

  5. ubuntu12.04和deepin12.06使用root账户登录

    修改lightdm.conf登录方式, 1.首先,激活root账户, 在终端中执行 sudo passwd root 连续两次输入 密码,即使root账户的密码,注意密码并不在终端中显示   2. 切 ...

  6. Linux下自带的regex

    Linux下可直接用regex.h来支持正则表达式. Android同样也有该头文件,可认为Android也是支持的. #include <sys/types.h> #include &l ...

  7. Unity加入Android广告小结

    在Unity游戏加入广告大致有以下几种方式: 导入Android Jar包 导出游戏为Android项目(在Build时选中Google Android Project,这种方法可以参考将Unity3 ...

  8. .NET开源工作流RoadFlow-流程设计-流程步骤设置-数据设置

    数据设置是控制在流程处理过程中,当前步骤的数据显示与编辑状态,控制当前步骤哪些字段为只读,隐藏或可编辑.需要配合表单设计器使用.

  9. windows phone版的一个儿教app

    昨天下午看见一个园友写的一篇关于儿教的api,看了也就两三个接口,所以数据处理应该不会太复杂,主要是界面的效果,要求可能比较高.于是我就重新自己写了一个app,实现很简单,花的时间比较多的地方应该是在 ...

  10. ubuntu中安装Rstdio无法切换中文输入法

    安装了RStudio,发现没法切换出中文输入法,搜索了一下 具体参考这里:https://support.rstudio.com/hc/en-us/articles/205605748-Using-R ...