Square

TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 1638   Accepted: 440

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

思路:从第一个数开始搜索,将其和与边长比对,相等则计数+1,计数达到3的时候说明可以组成,因为剩下那条必与边长相等,搜索过程注意剪枝,若某个数已被加入边长则不能重复计算,应将其标记,另外应在每一层递归时进行判断,看是否满足结束条件,以此来优化时间

#include<stdio.h>

#include<string.h>

int a[25],vis[25];

int con,temp,side,sum,flag,k;

  //con用来记录边数,temp存放暂时的边长,用来与目标边长比对,index是每次查找的起始点(从上次结束的位置),非常重要,用此优化时间

void dfs(int con,int temp,int index)

{

int i;
if(3==con)
{
flag =1;
return;
}
if(temp==side)
{
dfs(con+1,0,0);
if(flag)
return;
}
for(i = index ;i < k;i++)
{
if(!vis[i])    //判断此数是否已被用过
{
vis[i]=1;
dfs(con,temp+a[i],i+1);
if(flag)
return;
vis[i]=0;
}
}}int main(){
int n,m,max;
scanf("%d",&n);
while(n--)
{
k = sum =0;
flag = max =0;
memset(vis,0,sizeof(vis));
scanf("%d",&m);
while(m--)
{
scanf("%d",&a[k++]);
sum += a[k-1];
if(max<a[k-1])
max = a[k-1];
}
if(sum%4||max>sum/4)
{
printf("no\n");
continue;
}
side = sum/4;
dfs(0,0,0);
if(flag)
{
printf("yes\n");
continue;
}
printf("no\n");
}
return0;}

Square的更多相关文章

  1. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  2. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  3. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  4. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  5. OPEN CASCADE Gauss Least Square

    OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...

  6. OpenCascade Eigenvalues and Eigenvectors of Square Matrix

    OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...

  7. Leetcode: Matchsticks to Square && Grammar: reverse an primative array

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  8. Leetcode: Valid Word Square

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  9. Modified Least Square Method and Ransan Method to Fit Circle from Data

    In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...

  10. hdu 1398 Square Coins (母函数)

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

随机推荐

  1. 如何在windows server 2008的桌面上显示 我的电脑

    装完windows server2008 r2 x64后发现桌面只有一个回收站图标,这一点和xp 20003都差不多,但是xp 2003很容易就把桌面上的 我的电脑 我的文档 网上邻居找到,但是win ...

  2. Codevs 5059 一起去打CS

    5059 一起去打CS 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 钻石 Diamond 题目描述 Description 早就和lyk约好了去打cs,一直没找着时间,终于今天我家 ...

  3. 桶排序之python实现源码

    tmp = [] def bucket_sort(old): for i in range(len(old)): tmp.append([]) for i in old: tmp[int( i * l ...

  4. android 开源框架推荐

    同事整理的 android 开源框架,个个都堪称经典.32 个赞! 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)  JS ...

  5. php购物车原理

    <?php/*购物车原理在产品展示页面时(如 shop.php?id=888),点击购买或添加到购物车时,根据相应的产品标识符(如 id),查询相应的数据库,如果查询表示有此产品,用 $_SES ...

  6. css中 中文字体(font-family)的标准英文名称

    Mac OS的一些: 华文细黑:STHeiti Light [STXihei] 华文黑体:STHeiti 华文楷体:STKaiti 华文宋体:STSong 华文仿宋:STFangsong 儷黑 Pro ...

  7. linux 中 ‘|’的作用

    利用Linux所提供的管道符“|”将两个命令隔开,管道符左边命令的输出就会作为管道符右边命令的输入.连续使用管道意味着第一个命令的输出会作为 第二个命令的输入,第二个命令的输出又会作为第三个命令的输入 ...

  8. 如何在版本控制工具中管理Sencha Architect的項目

    根據數次痛苦的經歷結合stack overflow上的解答,發現原來還是可以使用svn.git之類的版本控制工具管理Sencha Architect生成的項目的. 具體的要點如下,假定項目記作{PRO ...

  9. RabbitMQ PHP操作类,守护进程及相关测试数据

    封装类如下: <?php /* * amqp协议操作类,可以访问rabbitMQ * 需先安装php_amqp扩展 */ class RabbitMQCommand{ public $confi ...

  10. 《C和指针》章节后编程练习解答参考——第10章

    10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...