Square
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的更多相关文章
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [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 ...
- OPEN CASCADE Gauss Least Square
OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...
- OpenCascade Eigenvalues and Eigenvectors of Square Matrix
OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...
- 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 ...
- Leetcode: Valid Word Square
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- 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 ...
- hdu 1398 Square Coins (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- c#md5与SHA1验证函数
/// <summary> /// MD5验证函数 /// </summary> /// <param name="fileName">文件的路 ...
- mysql笔记之集群
1.主从配置 #主从都要加入以下配置如下 [mysqld] log-bin=mysql-bin #主从要不一样 server-id=222 #在主上建立一个用户给从的用 GRANT REPLICATI ...
- Java中多线程的使用!!
简介: 1.要了解多线程,首先我们得先了解进程和线程.那么什么是进程?进程就是一个正在运行的程序分配内存让应用程序能够运行的叫做进程.那么什么又是线程呢?线程:在一个程序中,负责代码的执行 ...
- 国庆第六日(2014年10月6日11:51:15),node-webkit,理财产品
(1)node-webkit:一篇很好的入门文章.入门.系列. 在window下的打包和运行.大漠的一篇讲解文章 . (2)lighttable: 官网. (3)现在的理财产品,雨后春笋般冒出:宝点网 ...
- Codevs 1172 Hankson 的趣味题 2009年NOIP全国联赛提高组
1172 Hankson 的趣味题 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Hanks 博 ...
- Linux下U盘的格式化
一次系统装机带来的烦恼. 之前有一次装centos 系统 ,把一个centos4.8的系统刻录到了一个8G的U盘,之后是centos安装成功了 ,却发现电脑不认识U盘了,试了好多次也没有处理好,刚好今 ...
- 一次plsql 问题记录
环境 : window 7 x64 oracle 10.2g plsql 10.0.5 问题是 新装的 oracle10.2 plsql 一直连接不上 ,oracle_home 配置都对 .sql ...
- 二进制方式快速安装MySQL数据库命令集合
二进制方式快速安装MySQL数据库命令集合 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1.安装mysql ls mysql ...
- 【实习记】2014-09-01从复杂到简单:一行命令区间查重+长整型在awk中的bug
9月1号,导出sql文件后,想到了awk,但很复杂.想到了用sed前期处理+python排序比较的区间查重法.编写加调试用了约3小时. 9月2号,编写C代码的sql语句过程中,发现排序可以交m ...
- python相关博客
入门:http://www.pythontip.com/ Python之禅--大道至简 美胜于丑,显胜于隐,简胜于繁,繁胜于杂,平胜于迭,疏胜于密,读胜于写...名可名, 请常名 http://www ...