Square

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 <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[250], vis[205], sum, l, n; int dfs(int x, int pos, int len)
{
if (x == 3)
return 1;
int i;
for (i = pos; i >= 0; i--)
{
if (!vis[i])
{
vis[i] = 1;
if (len + a[i]<l)
{
if (dfs(x, i - 1, len + a[i]))
return 1;
}
else if (len + a[i] == l)
{
if (dfs(x + 1, n - 1, 0))
return 1;
}
vis[i] = 0;
}
}
return 0;
} int main()
{
int t, i, j;
scanf("%d", &t);
while (t--)
{
sum = 0;
scanf("%d", &n);
for (i = 0; i<n; sum += a[i], i++)
scanf("%d", &a[i]);
l = sum / 4;
memset(vis, 0, sizeof(vis));
sort(a, a + n);
if (l * 4 != sum || n<4 || l<a[n - 1])//剪枝
{
printf("no\n");
continue;
}
if (dfs(0, n - 1, 0))
printf("yes\n");
else
printf("no\n");
} return 0;
}

HDU1518 Square(DFS) 2016-07-24 15:08 49人阅读 评论(0) 收藏的更多相关文章

  1. Apache+tomcat的整合 分类: C_OHTERS 2014-05-07 15:08 293人阅读 评论(0) 收藏

    http://blog.csdn.net/stefyue/article/details/6918542 为什么要做这个整合呢?当然,首先想到是就是Apache和Tomcat的区别.正因为有区别,有各 ...

  2. Codeforces816A Karen and Morning 2017-06-27 15:11 43人阅读 评论(0) 收藏

    A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

  3. max_flow(Dinic) 分类: ACM TYPE 2014-09-02 15:42 94人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> #include<queue> #in ...

  4. HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏

    Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...

  5. HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏

    Zipper Problem Description Given three strings, you are to determine whether the third string can be ...

  6. 浅谈声明与定义的区别 分类: C/C++ 2015-06-01 15:08 157人阅读 评论(4) 收藏

    以下代码使用平台是VS2012. 清楚明白声明与定义是一名合格的程序猿的基本要求. 本人认为,C++编码过程中谈及"声明"和"定义"是因为我们要使用一个变量.类 ...

  7. Hadoop常见异常及其解决方案 分类: A1_HADOOP 2014-07-09 15:02 4187人阅读 评论(0) 收藏

    1.Shell$ExitCodeException 现象:运行hadoop job时出现如下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : at ...

  8. PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏

    Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  9. 苹果应用商店AppStore审核中文指南 分类: ios相关 app相关 2015-07-27 15:33 84人阅读 评论(0) 收藏

    目录 1. 条款与条件 2. 功能 3. 元数据.评级与排名 4. 位置 5. 推送通知 6. 游戏中心 7. 广告 8. 商标与商业外观 9. 媒体内容 10. 用户界面 11. 购买与货币 12. ...

随机推荐

  1. 【343】MathJax、LaTex、Mathml 数学公式

    参考:cnblog中添加数学公式支持 分类参考: 1. 基本功能 MathJax 我的LaTeX入门 MathJax basic tutorial and quick reference 分段函数:矩 ...

  2. nginx+redis+4个tomcat 负载均衡

    1,先配置nginx ,如果80接口被占用,且80 的端口又惹不起,参考:https://www.cnblogs.com/xiaohu1218/p/10267602.html 2,下载redis,并配 ...

  3. intelij创建MapReduce工程

    1.创建一个maven工程 2.POM文件 <?xml version="1.0" encoding="UTF-8"?><project xm ...

  4. Java ReentrantLock和synchronized两种锁定机制的对比

    多线程和并发性并不是什么新内容,但是 Java 语言设计中的创新之一就是,它是第一个直接把跨平台线程模型和正规的内存模型集成到语言中的主流语言.核心类库包含一个 Thread 类,可以用它来构建.启动 ...

  5. 慕课网access_token的获取

    access_token的接口是微信公众号一个基础接口,access_token接口微信公众号一个非常重要的接口 access_token是微信公众号的全局唯一票据,公众号的所有接口的调用都需要使用到 ...

  6. Java Tomcat下载、安装和环境变量配置

    win10下Tomcat的下载.安装和环境变量的配置 -----made by siwuxie095                             1.首先到Tomcat官网,传送阵:点击开 ...

  7. 22-maven-安装与配置

    转载:https://blog.csdn.net/wy727764020/article/details/80595451 Maven的安装以及eclipse中配置maven 2018年06月06日 ...

  8. redis中multi和pipeline区别以及效率(推荐使用pipeline)

    手册得知 pipeline 只是把多个redis指令一起发出去,redis并没有保证这些指定的执行是原子的:multi相当于一个redis的transaction的,保证整个操作的原子性,避免由于中途 ...

  9. Broadcast总结 service

    有时候离开应用就会接收不到系统的广播是因为系统默认发送的广播都会有一个参数 ntent startIntent = new Intent();startIntent.putExtra("pk ...

  10. 通过yiic来创建yii应用

    一.通过yiic来创建yii应用 (*yiic命令在yii下载包的framework目录下) 1.把你自已的php环境添加到系统环境变量中. 2.在命令行下输入: yiic webapp 位置\名称 ...