Square

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 4
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
 
Source

University of Waterloo Local Contest 2002.09.21

题意:根据已知边的长度,问能否构成一个正方形.

解决超时是关键!

AC代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[];
bool vis[];
int n,ave;
bool flag;
void dfs(int num,int len,int start)
{
if(flag)
return;
if(num==)
{
flag=true;
return;
}
if(len==ave)
{
dfs(num+,,);
if(flag)
return;
}
for(int i=start;i<n;i++)
{
if(!vis[i]&&len+a[i]<=ave)
{
vis[i]=true;
dfs(num,len+a[i],i+);
vis[i]=false;
if(flag)
return;
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
int sum=,maxlen=;
for(int i=;i<n;i++)
{
cin>>a[i];
sum+=a[i];
if(a[i]>maxlen)
maxlen=a[i];
}
ave=sum/;
if(sum%!=||maxlen>ave)
{
cout<<"no"<<endl;
continue;
}
sort(a,a+n);
memset(vis,,sizeof(vis));
flag=false;
dfs(,,);
if(flag)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
}
 

HDU1518 Square(DFS,剪枝是关键呀)的更多相关文章

  1. 1317: Square(DFS+剪枝)

    Description Given a set of sticks of various lengths, is it possible to join them end-to-end to form ...

  2. HDU-1518 Square(DFS)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  3. HDU1518:Square(DFS)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  4. HDU1518 Square 【剪枝】

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  5. 【DFS+剪枝】Square

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/J [题意] 给定n个木棍,问这些木棍能否围成一个正方形 [Accepted] # ...

  6. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  7. Zipper(poj2192)dfs+剪枝

    Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15277   Accepted: 5393 Descripti ...

  8. poj 1011 :Sticks (dfs+剪枝)

    题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...

  9. poj1011(DFS+剪枝)

    题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...

随机推荐

  1. sqlserver 连接远程数据库小结

    A,B两个数据库,不在同一台服务器实例 当需要通过sqlserver语句来实现对远程数据库操作(OPENDATASOURCE): select * from -- 操作类型 OPENDATASOURC ...

  2. MVC – 3.EF(Entity Framework)

    1.实体框架(EF)简介 与ADO.NET的关系 全称是ADO.NET Entity Framework,是微软开发的基于ADO.NET的ORM(Object/Relational Mapping)框 ...

  3. Pyqt QTabWidget 简单的计算器集合

    今天我们简单介绍下QTabWidget,然后在加入Demo计算器 首先我先讲下文件的结构: 文件分四部分, 一部分是Ui设计文件, 一部分是由Ui生成的py文件, 一部分是 计算器的逻辑文件,  最后 ...

  4. 【翻译十八】java-并发之锁对象

    Lock Objects Synchronized code relies on a simple kind of reentrant lock. This kind of lock is easy ...

  5. [LeetCode] Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. Howto: Connect MySQL server using C program API under Linux or UNIX

    From my mailbag: How do I write a C program to connect MySQL database server? MySQL database does su ...

  7. phpcms v9网站搬家更换域名的方法

    PHPCMS 是国内领先的网站管理系统,同时也是一个开源的PHP开发框架. 本文介绍phpcms v9网站搬家更换域名的方法. 1.在新的主机空间把phpcms安装好. 新安装的版本一定要和准备搬迁的 ...

  8. hdu 4036 2011成都赛区网络赛F 模拟 **

    为了确保能到达终点,我们需要满足下面两个条件 1.能够到达所有山顶 2.能够在遇到苦土豆时速度大于他 二者的速度可以用能量守恒定律做,苦土豆的坐标可通过三角形相似性来做 #include<cst ...

  9. excel、csv、txt文件数据读取

    /// <summary> /// 读取Excel表每一行第一列的字符串集合 /// </summary> /// <param name="filePath& ...

  10. pylab模式

    启动IPython时ipython --pylab就可以进入pylab模式,这种模式下画图时图片不会直接蹦出来,而是嵌在交互环境中,当然sypder里自动是pylab模式了