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
 题意 :给几组数据 看每组数据 能否所实用完 并组成一个正方形

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int q[22];
int vis[22];
int cmp(int a,int b)
{
return a<b;
}
int t,n,s;
void dfs(int num,int k,int length) //num 是已完毕的边 k是 数组的位置 length是当前边的长度
{
if(t==1)
return ;
if(num==4)
{
t=1;
return ;
}
if(length==s)
{
dfs(num+1,0,0);
if(t==1)
return ;
}
for(int i=k; i<n; i++)
{
if(length+q[i]<=s&&vis[i]==0)
{
vis[i]=1;
dfs(num,i+1,length+q[i]);
vis[i]=0;
}
}
}
int main()
{
int m;
scanf("%d",&m);
while(m--)
{
t=0;
s=0;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&q[i]);
s=s+q[i];
}
if(s%4!=0)
{
printf("no\n");
continue;
}
sort(q,q+n,cmp);
s=s/4;
if(q[n-1]>s)
{
printf("no\n");
continue;
}
dfs(0,0,0);
if(t==1)
printf("yes\n");
else
printf("no\n"); }
return 0;
}

标准的回溯  可惜 我没有看到这个问题 要运行数据

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 1518 Square 搜索的更多相关文章

  1. hdu 1518 Square(深搜+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...

  2. hdu 1518 Square 深搜,,,,花样剪枝啊!!!

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

  3. hdu 1518 Square 木棍建正方形【DFS】

    题目链接 题目大意: 题意就是输入棍子的数量和每根棍子的长度,看能不能拼成正方形. #include <bits/stdc++.h> using namespace std; int n, ...

  4. HDU 1518 Square(DFS)

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

  5. HDU 1518 Square

    解题思路:sum%4!=0    , max<sum/4 #include<iostream>#include<cstdio>#include<cstring> ...

  6. Square HDU 1518 搜索

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

  7. 杭电1518 Square(构成正方形) 搜索

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

  8. hdu 1518 拼正方形

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题意:输入几个长度,判断能否拼成正方形. 以下部分参考了网友代码,终于ac啦. #include ...

  9. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

随机推荐

  1. unity3D的FingerGestures小工具

    夹 FingerGestures包结构 FingerGestures样例列表 设置场景 教程:识别一个轻敲手势 教程:手势识别器 教程:轻击手势识别器 教程:拖拽手势识别器 教程:滑动手势识别器 教程 ...

  2. php-GD库的函数(二)

    <?php //imagecopy — 拷贝图像的一部分粘贴到某图像上 /*bool imagecopy ( resource $dst_im , resource $src_im , int ...

  3. 深入理解Oracle RAC 12c 笔记

    深入理解Oracle RAC 12c 跳转至: 导航. 搜索 文件夹 1 概述 2 集群件管理和故障诊断 3 执行实践 4 新特性 5 存储和ASM 6 应用设计上的问题 7 管理和调优一个复杂的RA ...

  4. hdu 2082 生成函数

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2082 找单词 Time Limit: 1000/1000 MS (Java/Others)    Me ...

  5. ecshop 调用其他数据库中的商品

    ecshop中修改includes/cls_ecshop.php中第53行 function table($str) { /* if($str=='goods'){ return '`ecshop3' ...

  6. app服务器

    http://heipark.iteye.com/blog/1847421http://heipark.iteye.com/blog/1847421http://wenku.baidu.com/vie ...

  7. sdut 6-2 多态性与虚函数

    6-2 多态性与虚函数 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1000ms Memory Limit ...

  8. 用EnableMenuItem不能使菜单变灰的原因

    为何不能Disable菜单项     问:我有一个工具button在WM_COMMAND消息是这样做的:         CMenu   *pMenu   =   GetMenu();     pMe ...

  9. 状态压缩dp(hdu2167,poj2411)

    hdu2167 http://acm.hdu.edu.cn/showproblem.php?pid=2167 给定一个N*N的板子,里面有N*N个数字,选中一些数字,使得和最大 要求任意两个选中的数字 ...

  10. Spring系列

    Spring系列之访问数据库   阅读目录 一.概述 二.JDBC API的最佳实践 三.Spring对ORM的集成 回到顶部 一.概述 Spring的数据访问层是以统一的数据访问异常层体系为核心,结 ...