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

这题题意是给你一些边,看能够构成正方形,这题的数据比较水,为后面的poj1011埋下伏笔。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int vis[30],liang,a[30],n;//liang表示每条边的长
bool cmp(int a,int b){
return a<b;
}
int dfs(int x,int pos,int len)//x表示已经拼了几根,pos表示下次从哪根开始拼,len表示当前拼的这根已经拼了多少长度
{
int i,j;
if(x==3)return 1;
for(i=pos;i>=1;i--){
if(!vis[i]){
if(a[i]+len<liang){
vis[i]=1;
if(dfs(x,i-1,len+a[i]))
return 1;
vis[i]=0;
}
else if(a[i]+len==liang){
vis[i]=1;
if(dfs(x+1,n,0))return 1;
vis[i]=0; }
}
}
return 0;
} int main()
{
int m,i,j,T,sum;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
if(sum%4!=0){
printf("no\n");continue;
}
liang=sum/4;
sort(a+1,a+1+n,cmp);
memset(vis,0,sizeof(vis));
if(dfs(0,n,0))printf("yes\n");
else printf("no\n");
}
return 0;
}

poj2362 Square的更多相关文章

  1. poj2362 Square(DFS)

    题目链接 http://poj.org/problem?id=2362 题意 输入n根棍子的长度,求这n根棍子是否能组成一个正方形. 思路 假设能组成正方形,则正方形的周长为sum,sum/4为正方形 ...

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

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

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

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

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

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

  5. [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 ...

  6. OPEN CASCADE Gauss Least Square

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

  7. OpenCascade Eigenvalues and Eigenvectors of Square Matrix

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

  8. 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 ...

  9. Leetcode: Valid Word Square

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

随机推荐

  1. 怎么判断是旧版本的ext3还是新版本?

    怎么判断是旧版本的ext3还是新版本的?   ---高性能419

  2. 基于numpy.einsum的张量网络计算

    张量与张量网络 张量(Tensor)可以理解为广义的矩阵,其主要特点在于将数字化的矩阵用图形化的方式来表示,这就使得我们可以将一个大型的矩阵运算抽象化成一个具有良好性质的张量图.由一个个张量所共同构成 ...

  3. 【Linux】CentOS7中修改中文字符集

    CentOS 7中字符集查看的方式是 locale -a   或者locale 如果想显示中文的话,应该修改为 LANG="zh_CN.UTF-8" 在命令行界面临时修改字符集的话 ...

  4. wmic process进程管理

    process    进程管理工具 示例:1.列举当前的进程.进程路径.命令行.进程ID.父进程ID.线程数,内存使用::wmic process get name,executablepath,co ...

  5. SAP 修改数据元素 注意事项

    在修改数据元素的时候,通常要注意一下几点: 1.在修改完数据元素后,如果激活不成功,那么就要通过SE14进入数据库实用程序,在对象名处输入数据元素相关联的表的名称 下面词典对象选择表,然后点击编辑,处 ...

  6. floating point

    记录浮点数的单精度和双精度(IEEE754) 1.单精度(float) ​ 1.定义:单精度占4字节/32位,其中1号位符号位,其次是8位阶码/指数(阶符+阶数),23位尾数(小数). 2.双精度(d ...

  7. 通过电脑浏览器调试真机h5兼容问题

    前言 在h5开发过程中,起初我们使用PC浏览器的手机模式打开开发中的页面,并使用控制台进行调试,但实际真机兼容性问题无法调试到:在这种情况下,我们通常使用vConsole(即移动端的控制台)来调试,但 ...

  8. 极光推送的设备唯一性标识 RegistrationID

    极光推送的设备唯一性标识 RegistrationID 极光推送的设备唯一性标识 RegistrationID | 极光博客 https://blog.jiguang.cn/registrationi ...

  9. mysql本地中127.0.0.1连接不上数据库怎么办

    首先在本地使用Navicat for MySQL建立一个bai数据库.在dreamweaver中建立一个PHP格式的网页,方便链接测试.测试发du现,如果zhi无法使用localhost链接mysql ...

  10. Box Model 盒子模型

    Box Model盒子模型,是初学者在学习HTMl5时会学到的一个重要的模型,也有一些人称它为框模型,因为盒子是属于3维,而框是平面的.称之为盒子模型,是因为其结构和盒子十分相似,其最外面是margi ...