题目描述:

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
 
代码如下:
 /*要剪枝,当所有木棒总长度不能被4整除时以及木棒最大长度大于总长度除以4时,
* 不能组成正方形,直接输出no
* 深搜时从第一个开始往后搜索,只要满足当前边长+当前木棒长<正方形边长,
* 就标记该木棒,并继续搜索后面的木棒,当木棒长度=sum/4 时,count加1,
* 当count=3时表明能够成正方形,flag=1,返回,flag=0则不能组成正方形。*/
#include<iostream>
#include<cstring> int visit[];
bool flag;
int number[];
int n,len; using namespace std; void dfs(int cur,int pos,int count);
int main()
{
int m;
cin >> m;
while(m--)
{
int max = ,sum = ;
cin >> n;
memset(visit,,sizeof(visit));
for(int i = ;i < n;i++)
{
cin >> number[i];
sum += number[i];
if(number[i] > max)
max = number[i];
}
len = sum / ;
if(sum % != || max > len)//不能总和不能被4整除或最大值大于平均值
{
cout << "no" << endl;
continue;
}
flag = ;
dfs(,,);
if(flag)
cout << "yes" << endl;
else
cout << "no" << endl;
}
} void dfs(int cur,int pos,int count)
{
if(cur == len)//木棍相加的值等于平均值
{
count++;
cur = ;//当一边成立时,要考虑另外一边注意将cur清0
pos = ;
if(count == )//当有三边成立时
{
flag = ;
return;
}
}
for(int i = pos;i < n;i++)
{
if(!visit[i])//还没有被访问过
{
if((cur + number[i]) <= len)//加上木棍后,长度小于或等于平均值
{
visit[i] = ;
dfs(cur + number[i],i,count);
if(flag)//一旦有成立的,跳过剩下的判断
return;
visit[i] = ;//回溯
}
}
}
}

代码分析:

这道题目要注意的地方就是剪枝。

参考地址:http://www.cnblogs.com/PegasusWang/archive/2013/04/08/3008942.html

Square(hdu 1511)的更多相关文章

  1. Square HDU 1518 搜索

    Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...

  2. HDU 1535 Invitation Cards (POJ 1511)

    两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...

  3. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  4. hdu 5079 Square

    http://acm.hdu.edu.cn/showproblem.php?pid=5079 题意: n*n网格,每个格子可以涂黑色或白色,有的格子必须涂黑色 问最大白色正方形边长分别为0,1,2,… ...

  5. hdu 1398 Square Coins 分钱币问题

    Square Coins Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  6. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

  7. HDU 6125 - Free from square | 2017 Multi-University Training Contest 7

    思路来自这里 - - /* HDU 6125 - Free from square [ 分组,状压,DP ] | 2017 Multi-University Training Contest 7 题意 ...

  8. hdu 1398 Square Coins (母函数)

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

  9. hdu 1398 Square Coins(简单dp)

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

随机推荐

  1. VS2010/MFC对话框:一般属性页对话框的创建及显示

    一般属性页对话框的创建及显示 本节将介绍一般属性页对话框的创建和显示. 实际上,一般属性页对话框的创建和显示过程和向导对话框是很类似的.鸡啄米将上一节中的向导对话框进行少量修改,使其成为一般属性页对话 ...

  2. aliyun 镜像

    [epel]name=Extra Packages for Enterprise Linux 6 - $basearchbaseurl=http://mirrors.aliyun.com/epel/6 ...

  3. ollicle.com: Biggerlink – jQuery plugin

    ollicle.com: Biggerlink – jQuery plugin Biggerlink – jQuery plugin Purpose Demo Updated for jQuery 1 ...

  4. hdu-Common Subsequence

    http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description A subsequence of a given sequence ...

  5. Boost源代码学习---shared_ptr.hpp

    最近观看Boost库源代码.Boost功能强大的库,但它的许多源代码,十一细读太费时间,毕竟,还有其他东西要学.所以我决定脱脂感兴趣的章节,他们的设计思路和难以理解的地方记录. shared_ptr是 ...

  6. SQL Server索引进阶:第五级,包含列

    原文地址: Stairway to SQL Server Indexes: Level 5, Included Columns 本文是SQL Server索引进阶系列(Stairway to SQL ...

  7. AngularJs 简单入门

    1.AngularJs 是什么以及应用程序组成的三部分 AngularJS是一个开发动态Web应用的框架.它让你可以使用HTML作为模板语言并且可以通过扩展的HTML语法来使应用组件更加清晰和简洁.它 ...

  8. windows hook (转)

    http://blog.csdn.net/friendan/article/details/12226201 原文地址:http://blog.sina.com.cn/s/blog_628821950 ...

  9. XHTML CSS 常见问题和解决方案

    原文地址:XHTML CSS 常见问题和解决方案 作为前端开发人员,在日常的页面制作时,不可避免的会碰上这样那样的问题,我挑选了其中的一些进行总结归档,希望对大家会有所帮助: 1.如何定义高度很小的容 ...

  10. web 性能优化指南阅读笔记

    1.关于拥塞预防算法 PRR-比例降速,RFC6937 规定的一个新算法,其目标是改进丢包后的恢复速度,谷歌测量结果:该算法改进丢包造成的平均连接延迟减少了3%-10%.PRR是linux 3.2+内 ...