题目描述:

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. javascript操作JSON字符

    1.先要区分JSON字符串和JSON对象 JSON字符串: Var strJSON = “{“Area”:[{“AreaId”:”123”},{“AreaId”:”345”}]}”,   或者 Var ...

  2. zoj 3706 Break Standard Weight

    /*题意:将两个砝码中的其中一个分成两块,三块组合最多有几种情况(可以只有一块,或者两块). 组合情况 i j m 三块砝码 (i+j)-m=m-(i+j) i+j i-j=j-i  i j m (i ...

  3. VS C++工程类成员初始化检测脚本

    最近项目中出现由类成员未初始化而进行读写而造成的问题,于是想将项目中所有的为初始化的地方找出来,优化一下代码,维护了这么多年的程序已有百万余行且VS2015还尚未支持检查类成员初始化的方法.,于是想写 ...

  4. (转)原子操作 Interlocked系列函数

    上一篇<多线程第一次亲密接触 CreateThread与_beginthreadex本质区别>中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是 ...

  5. Windows NTService 后台框架封装

    对于后台运行的程序,比如基于C/S架构的服务器.各种监控系统的程序.或者是程序额外的功能需要后台运行来实现,在Windows平台中,服务经常会用到,这种对于需要24×7运行的程序是必备的,至少本人经常 ...

  6. C语言的指针变量

    C语言的指针变量 在C语言中,变量是固定范围的存储空间,它存储的是赋给他的值, 比如: ; /* 这里是定义一个整型变量a,并把12这个值存储在a的地址空间上 这个地址空间是系统随机分配的,对用户是透 ...

  7. 创建txt格式文本日志

    公共方法(可以将其放到类库里边): #region 记录日志 #region 写日志 /// <summary> /// 写日志 /// </summary> /// < ...

  8. java断言

    public class New{ public static void main(String[] args){ assert false; System.out.println("pas ...

  9. Firefox 备份

    参考http://mozilla.com.cn/post/32327/ 火狐的地址栏中输入about:support点击“打开所在文件夹”按钮,会弹出一个资源管理器,并且定位到你当前的Profile文 ...

  10. # void :;

    href="#"---->top 连续点击的时候会出bug javascri中的void是一个操作符,该操作符指定要计算一个表达式但是不返回值. javascript:voi ...