题目描述:

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. MyEclipse13中修改Servlet.java源代码

    Servlet.java源代码想要修改的步骤,与低版本的不同废话少说,直接来步骤: 1,在myEclipse的安装目录中搜索com.genuitec.eclipse.wizards文件,如图:选择co ...

  2. qt4.8.4安装以及64位程序编译方法

    本文将使用简单的几个步骤说明在vc2008和64位的操作系统下如何编译安装x64Qt软件 首先必须保证你所使用的系统是64bit的操作系统,本次我们使用的系统是windows7 professiona ...

  3. Cookie获取、设置值

    设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...

  4. HTML5入门(一)

    HTML简单介绍: HTML(HyperText Markup Language),超文本标记语言,是一种专门用于创建web的超文本文档编程语言,是我们看到的网页的源代码. 版本简介: 1997年推出 ...

  5. SSIS DB目录设置 (Integration Services Catalogs)

    1.创建SSISDB目录 这里没什么好说的,点击Enable CLR Integration ,然后设置一个加密密码 2. SSIS Catalog设置 Retention Period (days) ...

  6. BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

    有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...

  7. HDU3496-Watch The Movie

    描述: New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight ...

  8. php基础知识(每天分享一些以前的笔记希望能帮助自学的朋友)

    php基础(第一天) php标签 1.  要知道php是一种嵌入html文档的脚本语言:php语法格式是:<?php 想要写的内容 ?>红色体就是php的标签,注意这些标签都要在英式输入法 ...

  9. mybatis+spring+c3p0+maven+ehcache

    项目截图 pom.xml如下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...

  10. discuz默认模板文件结构详解-模板文件夹介绍

    | — template — default   系统内置风格模板(默认风格)| — template — default  – discuz_style_default.xml  风格安装文件,可用 ...