思路:从第一个数开始搜索,将其和与边长比对,相等则计数+1,计数达到3的时候说明可以组成,因为剩下那条必与边长相等,搜索过程注意剪枝,若某个数已被加入边长则不能重复计算,应将其标记,另外应在每一层递归时进行判断,看是否满足结束条件,以此来优化时间



#include<stdio.h>

#include<string.h>

int a[25],vis[25];

int con,temp,side,sum,flag,k;

  //con用来记录边数,temp存放暂时的边长,用来与目标边长比对,index是每次查找的起始点(从上次结束的位置),非常重要,用此优化时间

void dfs(int con,int temp,int index)

{

int i;
if(3==con)
{
flag =1;
return;
}
if(temp==side)
{
dfs(con+1,0,0);
if(flag)
return;
}
for(i = index ;i < k;i++)
{
if(!vis[i])    //判断此数是否已被用过
{
vis[i]=1;
dfs(con,temp+a[i],i+1);
if(flag)
return;
vis[i]=0;
}
}}int main(){
int n,m,max;
scanf("%d",&n);
while(n--)
{
k = sum =0;
flag = max =0;
memset(vis,0,sizeof(vis));
scanf("%d",&m);
while(m--)
{
scanf("%d",&a[k++]);
sum += a[k-1];
if(max<a[k-1])
max = a[k-1];
}
if(sum%4||max>sum/4)
{
printf("no\n");
continue;
}
side = sum/4;
dfs(0,0,0);
if(flag)
{
printf("yes\n");
continue;
}
printf("no\n");
}
return0;}

HDU 1518的更多相关文章

  1. Square HDU 1518 搜索

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

  2. hdu 1518 拼正方形

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

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

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

  4. hdu 1518 深搜

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  5. HDU 1518 Square

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

  6. HDU 1518 Square 搜索

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

  7. hdu 1518 BFS

    Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? I ...

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

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

  9. HDU 1518 Square(DFS)

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

随机推荐

  1. 让c像python一样可以在命令行写代码并且编译

    在你亲爱的.bashrc/.zshrc中添加 ###C###go_libs="-lm"go_flags="-g -Wall -include allheads.h -O3 ...

  2. iOS极光推送集成步骤

    1.下载SDK,导入Xcode 2.在苹果开发者中心建立AppId与bundleID进行关联,注意勾选推送功能 3.在苹果开发者中心建立推送证书 4.在极光后台建立应用且上传推送证书 5.建立描述文件 ...

  3. MongoDB入门三步曲2--基本操作(续)--聚合、索引、游标及mapReduce

    mongodb 基本操作(续)--聚合.索引.游标及mapReduce 目录 聚合操作 MapReduce 游标 索引 聚合操作 像大多关系数据库一样,Mongodb也提供了聚合操作,这里仅列取常见到 ...

  4. nginx 常用的 URL 重写方法

    转自:http://www.jbxue.com/article/4727.html Nginx中一些常用的URL 重写方法介绍,有需要的朋友可以参考下.url重写应该不陌生,不管是SEO URL 伪静 ...

  5. jquery 循环显示div的示例代码

    我们用一个语句就让下面五个div显示成功,具体实现如下,感兴趣的朋友可以参考下 直接看例子 复制代码代码如下: for(var p=1; p<=5; p++){  $("#proper ...

  6. php查询ip地址来源归属地的脚本

    <?php header('Content-Type:text/html;charset=utf-8'); if($_GET['sub']){ $ip = $_GET['ip']; $msg = ...

  7. js电话号码正则校验--座机和手机号

    1.最新的电话号码段: 移动:134(1349除外)135 136 137 138 139 147 150 151 152 157 158 159 182 183 184 187 188 联通: 13 ...

  8. JAVA获取当前日期以及将字符串转成指定格式的日期

    /* * To change this template, choose Tools | Templates * and open the template in the editor. */ pac ...

  9. mongodb MongoDB 聚合 group

    MongoDB 聚合 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 基本语法为:db.col ...

  10. ECSHOP安装或使用中提示Strict Standards: Non-static method cls_image:

    随着ECSHOP的不断发展,越来越多的人成为了ECSHOP的忠实粉丝.由于每个人的服务器环境和配置都不完全相同,所以ECSHOP也接二连三的爆出了各种各样的错误信息.相信不少新手朋友在ECSHOP安装 ...