Square

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

Total Submission(s): 11824    Accepted Submission(s): 3794
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
 有n支棍子,是否可以组成一个正方形
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int s[21],n,sum,target;
bool used[21];
int cmp(int a,int b)
{
return a>b;
}
bool dfs(int curs,int curl,int pos)
{//curs:找到了几条正方形的边,curl:当前边已经拼凑的长度,只用pos之后的
if(curs==3)
return 1;//有了三条边,第四条肯定也是存在的
for(int i=pos;i<n;i++)
{
if(used[i]==true)
continue;
if(curl+s[i]==target)
{
used[i]=true;
if(dfs(curs+1,0,0)==true)//找到一条边之后就找下一条边
return true;
used[i]=false;
}
else if(curl+s[i]<target)
{
used[i]=true;
if(dfs(curs,curl+s[i],i)==true)
return true;
used[i]=false;//回溯 ,一条边可用可不用
}
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(s,0,sizeof(s));
scanf("%d",&n);
sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&s[i]);
sum+=s[i];
}
target=sum/4;
if(sum%4!=0||n<4)
cout<<"no"<<endl;
else
{
memset(used,false,sizeof(used));
sort(s,s+n,cmp);
if(target<s[0])
cout<<"no"<<endl;
else if(dfs(0,0,0)==true)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
}
return 0;
}

hdoj--1518--Square(dfs)的更多相关文章

  1. HDU 1518 Square(DFS)

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

  2. HDOJ.1342 Lotto (DFS)

    Lotto [从零开始DFS(0)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tempter of ...

  3. hdoj 1518 Square 【dfs】

    题意:给出n个(不同长度的)棍子,问能不能将他们构成一个正方形. 策略:深搜. hdoj 1455的简化版 代码: #include <stdio.h> #include <stri ...

  4. HDU1518 Square(DFS)

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

  5. HDU1518 Square(DFS) 2016-07-24 15:08 49人阅读 评论(0) 收藏

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

  6. poj2362 Square(DFS)

    题目链接 http://poj.org/problem?id=2362 题意 输入n根棍子的长度,求这n根棍子是否能组成一个正方形. 思路 假设能组成正方形,则正方形的周长为sum,sum/4为正方形 ...

  7. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

  8. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  9. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  10. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

随机推荐

  1. db2 jdbc连接字符串中 指定currentSchema

    场景:连接DB2数据库的,jdbc的连接字符串中没有给当前的数据源用户指定默认的schema,而当前的数据源用户下可能有多个schema,则会使用数据源用户默认的schema. 例如:admin用户的 ...

  2. OpenCV:使用OpenCV3随机森林进行统计特征多类分析

    原文链接:在opencv3中的机器学习算法练习:对OCR进行分类 本文贴出的代码为自己的训练集所用,作为参考.可运行demo程序请拜访原作者. CNN作为图像识别和检测器,在分析物体结构分布的多类识别 ...

  3. WPF中的两个绑定场景

    1. 如何在诸如ListBox这样的项中绑定父类数据上下文. <ListBox Grid.Row=" ItemsSource="{Binding Entries}" ...

  4. Lazarus Coolbar and AnchroDocking

    在lazarus1.6里加载了AnchroDocking后,Coolbar突然不见了,找了好久没找到,原来在这里! 在AnchroDocking中可能是为了界面的最大化,默认是开始Toolbar 而关 ...

  5. NSOperationQueue和GCD的区别,以及在什么场合下使用

    1> GCD是纯C语言的API .NSOperationQueue是基于GCD的OC的封装. 2> GCD只支持FIFO队列,NSOperationQueue可以方便设置执行顺序,设置最大 ...

  6. 怎么设置font awesome图标的大小?

    <i class="fa fa-camera-retro fa-lg"></i> fa-lg <i class="fa fa-camera- ...

  7. js的一些老司机写法

    //取整 parseInt(a,10); //Before Math.floor(a); //Before a>>0; //Before ~~a; //After a|0; //After ...

  8. Linux命令(文本编辑器)

    vi和vim编辑器:有插入模式,一般模式,地行模式 一班模式通过(i.a.o.I.A.O)键--->进入插入模式            插入模式(按Esc键退出)---->j进入一班模式 ...

  9. 6.shell脚本

    6.1 shell基础语法 6.1.1 shell的概述 shell的基本概念 1.什么是shell shell是用户和Linux操作系统之间的接口,它提供了与操作系统之间的通讯方式 shell是一个 ...

  10. Maven中更改默认JDK版本

    只要在settings.xml文件中加上如下标签即可.(我这里是默认的1.7版本) <profiles> <profile> <id>jdk-1.7</id& ...