Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11151    Accepted Submission(s): 3588

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
 
题意: 给一列数, 问能否把这列数分成四组, 且每组数的和相同。
 
解法: 有DFS进行回溯。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int a[], n, ans;
bool vis[]; int dfs(int cur, int len, int pos)//cur表示用过的个数, len表示长度, pos表示位置
{
if(cur==n)
return ;
for(int i=pos; i<n; i++)
{
if(vis[i]) continue;
if(len+a[i]<ans)
{
vis[i] = ;
if(dfs(cur+, len+a[i], i+)) return ;
vis[i] = ;
if(len==) return ;
while(a[i]==a[i+]&&i+<n) ++i;//剪枝
}
else if(len+a[i]==ans)
{
vis[i] = ;
if(dfs(cur+, , )) return ;
vis[i] = ;
return ;
}
}
return ;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int Sum = ;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d", &a[i]);
Sum+=a[i];
}
if(Sum%)
{
printf("no\n");
continue;
}
ans = Sum/;
memset(vis, , sizeof(vis));
int temp = dfs(, , );
printf("%s\n", temp?"yes":"no");
}
return ;
}

HDU1518 Square(DFS)的更多相关文章

  1. 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 ...

  2. HDU 1518 Square(DFS)

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

  3. poj2362 Square(DFS)

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

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

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

  5. LeetCode Subsets II (DFS)

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

  6. LeetCode Subsets (DFS)

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

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

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

  8. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  9. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

随机推荐

  1. Terminal的快捷键 for Terminal for Mac OS 10.10, Linux/GNU(Ubuntu, deepin, elementory os,CentOS)

    对于习惯用windows键盘的,突然转成Mac蓝牙键盘真的有点不习惯,尤其是多了⌘这个键,还有Alt键也成了Option 但是对于Windows下熟悉的快捷键,它们真的失效了,还好Ubuntu也常用, ...

  2. A4纸网页打印——宽高设置

    一.在公制长度单位与屏幕分辨率进行换算时,必须用到一个DPI(Dot Per Inch)指标. 经过我仔细的测试,发现了网页打印中,默认采用的是96dpi,并非传闻的72dpi . A4纸张的尺寸是2 ...

  3. Oracle通过sqlplus spool导入导出数据

    第一部分(实例,主要分两步),第二部分(参数小总结),第三部分(完全参数总结) 第一部分 第一步 :这是我的导出数据的脚本call.sqlconn scott/tigerset echo offset ...

  4. Oracle之Union与Union all的区别

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并在一起显示出来. union和unio ...

  5. ThreadLocal的使用及介绍

    ThreadLocal总结 1.ThreadLocal使用场合主要解决多线程中数据数据因并发产生不一致问题.ThreadLocal为每个线程的中并发访问的数据提供一个副本,通过访问副本来运行业务,这样 ...

  6. 单链表操作B 分类: 链表 2015-06-07 12:42 15人阅读 评论(0) 收藏

    数据结构上机测试2-2:单链表操作B TimeLimit: 1000ms Memory limit: 65536K 题目描述 按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除 ...

  7. CSUFT 1002 Robot Navigation

    1002: Robot Navigation Time Limit: 1 Sec      Memory Limit: 128 MB Submit: 4      Solved: 2 Descript ...

  8. 从客户端中检测到有潜在危险的Request.Form 值

    今天往MVC中加入了一个富文本编辑框,在提交信息的时候报了如下的错误:从客户端(Content="<EM ><STRONG ><U >这是测试这...&q ...

  9. #在lua中的运用

    在lua中"#"表示返回表长度或字符串长度 例子一: a = "Hello " b = "World" print("Concat ...

  10. easyui commbox嵌入一个checkbox的实现

    function InitComBoBox(datagrid, combxid, formid, url, valueField, textField,_prompt) { $(combxid).co ...