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. cocos2d-x 贝塞尔曲线的简单运用(CCBezierTo,CCBezierBy)

    原文链接:http://blog.csdn.net/we000636/article/details/8616355 一.贝赛尔曲线简单介绍 贝塞尔曲线是应用于二维图形应用程序的数学曲线.曲线的定义有 ...

  2. python六核心编程——条件和循环

    1.if声明 单 if 通过使用布尔运算符的声明 and , or 和 not. if-elif-else. elif即else if if expression1:      expr1_true_ ...

  3. Java字节流和字符流

    file.txt文本中存储的内容: 好abc 1.字符流处理: package com.wjy.java; import java.io.FileInputStream; import java.io ...

  4. 用java代码实现环圈报数

    环圈报数就是围一圈人,每一次数数数到三的人自动出圈,再接着数, 用数据结构的思想实现 public class Count3Quit {     public static void main(Str ...

  5. Cocos2d-x3.0 Button

    Size widgetSize = Director::getInstance()->getWinSize(); Text* alert = Text::create("Layout& ...

  6. Failed to load libGL.so问题解决

    Ubuntu 14.04下启动模拟设备Android 4.2.2的时候报错: failed to load libgl.so 先用locate 命令定位libGL库, 然后加入�一个链接就可以: de ...

  7. docker 的安装

    官方站点上有各种环境下的 安装指南,这里主要介绍下Ubuntu和CentOS系列的安装. Ubuntu 系列安装 Docker 通过系统自带包安装 Ubuntu 14.04 版本号系统中已经自带了 D ...

  8. Balsamiq Mockups注册码

    Name: personalKey: eJzzzU/OLi0odswsqilILSrOz0vMqbFEAjXONYY1fu6ufgA/CA4X Name: helloWorldKey: eJzzzU/ ...

  9. 运行一个android程序,直接访问某个网站

    package android.com.youtube; import android.app.Activity; import android.content.Intent; import andr ...

  10. HTTPDNS成为移动互联网的标配–原因与原理解析(转)

    DNS,作用就是将域名解析成IP.一个DNS查询,先从本地缓存查找,如果没有或者已经过期,就从DNS服务器查询,如果客户端没有主动设置DNS服务器,一般是从服务商DNS服务器上查找.这就出现了不可控. ...