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. 我眼中真正优秀的CTO

    该文转自“肉饼铺子”.作者robbin是前JavaEye网站的创始人,TOPITCLUB互联网俱乐部发起人.  原文链接 现在进入正题,最近几个月,不断有人找我推荐CTO人选,这两年互联网创业和创投实 ...

  2. 项目报错-无法解析类型 XXXX.xx 从必需的 .class 文件间接引用了它

    这个编译错误有几个原因 1.jdk版本问题 要是报错是某些java包里的东西那就可以肯定是jdk版本的问题, 比如无法解析类型java.lang.Object,无法解析类型java.lang.Char ...

  3. 【制作镜像Win*】文件准备

    mkdir /var/image-createcd /var/image-create/ 在物理机上: wget http://10.254.3.75/images/libvirt/libvirt.x ...

  4. C++动态分配内存

    动态分配(Dynamic Memory)内存是指在程序运行时(runtime)根据用户输入的需要来分配相应的内存空间. 1.内存分配操作符new 和 new[] Example: (1)给单个元素动态 ...

  5. 程序员的编辑器——VIM

    from:http://blog.chinaunix.net/uid-11278770-id-148579.html Chinaunix首页 | 论坛 | 认证专区 | 博客 登录 | 注册      ...

  6. 接口Interface

    接口的定义 声明接口的方式与声明类的方式相似,但使用的关键字是interface,而不是class interface IMyInterface { // 接口成员 } 访问修饰符关键字public ...

  7. HTML5格式化

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. PHP设计模式之工厂/单例/注册者模式

    工厂模式 简单工厂模式 [静态工厂方法模式](Static Factory Method)是类的创建模式 工厂模式的几种形态: 1.简单工厂模式(Simple Factory)又叫做 静态工厂方法模式 ...

  9. 离开ACM了,总结一下

    写这篇博客,一如当初我对着电脑显示器,不知道从哪里下手才是,所以没准写着写着就出现了倒叙插叙补叙等充满语文功底的修辞手法,不过不会有45度的妩媚和忧伤. 像一位程序员所说:今天的努力是为了儿时吹过的牛 ...

  10. App评分

    //应用实现评论跳转的两种方法: //第一种: //在iOS6.0前跳转到AppStore评分一般是直接跳转到AppStore评分 //NSString *evaluateString = [NSSt ...