题目描述:

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. 合作开发,导入MyEclipse项目报错问题

    因工作原因,同事将他的java项目交接给了我.和平时的交接一样.他把他最新的源代码,打成压缩包,发给我.我解压后,使用myeclipse开发工具,通过导入,将项目导入到我的开发工具中,这个时候有一个问 ...

  2. HBA简介及原理

    HBA,即主机总线适配器英文“Host Bus Adapter”缩写.是一个使计算机在服务器和存储装置间提供输入/输出(I/O)处理和物理连接的电路板和/或集成电路适配器. 简介 主机总线适配器(Ho ...

  3. BNUOJ29065鸣人的查克拉

    鸣人的查克拉 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: ...

  4. Hibernate中,left join、inner join以及left join fetch区别(转)

    标签: hibernate hql inner join left right 杂谈 分类: SQL 原文地址:http://m33707.iteye.com/blog/829725 Select F ...

  5. 我用过的Linux命令--关闭防火墙

    关闭防火墙: 防火墙的弄能是限制某一些端口的使用,可以通过linux命令关系它,相应的指令: 查看防火墙信息: #service iptables status 就能看到防火墙的状态: 关闭防火墙: ...

  6. Java中Calender引用类型

    某些时候需要使用深拷贝: Calendar startTime = (Calendar) this._paramModel.getStartTime().clone(); 这样对startTime.a ...

  7. 7.PHP 教程_PHP常量

    常量值被定义后,在脚本的其他任何地方都不能被改变. PHP常量 常量是一个简单值的标识符.该值在脚本中不能改变. 一个常量由英文字母.下划线.和数字组成,但数字不能作为首字母出现.(常量名不需要加$修 ...

  8. PHP学习笔记12-上传文件

    上传图片文件并在页面上显示出图片 enctype介绍:enctype属性指定将数据发回到服务器时浏览器使用的编码类型. 取值说明: multipart/form-data: 窗体数据被编码为一条消息, ...

  9. 创建理想的SEQUENCE和自增长的trigger

    SEQUENCE CREATE SEQUENCE TEST_SEQ START 1 --从1开始,第一个一定是NEXTVAL,因为第一个CURRVAL不好使,返回值会是1,第一个NEXTVAL相当于从 ...

  10. Javascript中的位运算符和技巧

    ECMAScript 整数有两种类型,即有符号整数(允许用正数和负数)和无符号整数(只允许用正数).在 ECMAScript 中,所有整数字面量默认都是有符号整数,这意味着什么呢? 有符号整数使用 3 ...