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. 获取月份对应的day

    function getDaysInMonth(month, year) { , , , , , , , , , , , ]; //主要处理二月份的天数 ) && (year % == ...

  2. 编译mgiza的准备

    cmake之前需要首先设置环境变量: export BOOST_LIBRARYDIR=$BOOST_ROOT/lib64export BOOST_ROOT=/home/noah/boost_1_57_ ...

  3. IO - 同步,异步,阻塞,非阻塞 (转)

    转自:http://blog.csdn.net/historyasamirror/article/details/5778378 向大牛学习,言归正传.同步(synchronous) IO和异步(as ...

  4. 转载:监控每个节点(Indices部分)

    集群的健康只是一个方面,它是对整个集群所有方面的一个很高的概括.节点状态的api是另外一个方面,它提供了关于你的集群中每个节点令你眼花缭乱的统计数据. 节点的状态提供了那么多的统计数据,在你很熟悉它们 ...

  5. [备忘]Asp.net MVC 将服务端Model传递的对象转为客户端javascript对象

    <script type="text/javascript"> var jsObject = @Html.Raw(Json.Encode(Model.Objects)) ...

  6. scjp考试准备 - 6 - 父类构造器的引用

    题一,如下代码的执行结果: class Person{ String name = "No name"; public Person(String nm){name = nm;} ...

  7. [转]WinExec、ShellExecute和CreateProcess及返回值判断方式

    [转]WinExec.ShellExecute和CreateProcess及返回值判断方式 http://www.cnblogs.com/ziwuge/archive/2012/03/12/23924 ...

  8. 服务器 IIS 发布网站 支持下载 apk 和 ipa

    方法/步骤   1 打开IIS服务管理器,找到服务器,右键-属性,打开IIS服务属性: 2 单击MIME类型下的"MIME类型"按钮,打开MIME类型设置窗口: 3 单击" ...

  9. 快速生成R语言报告(markdown+Rstudio)

    先预览一下用Markdown写的报告[http://rpubs.com/loness/167347],这是HTML格式,你也可以导出Word.pdf,但是导出pdf时文中不能有中文,但是可以使用“pd ...

  10. UIDatePicker swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...