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. Java中删除指定文件夹文件夹下面有内容也删除使用递归方案

    import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java ...

  2. SPOOL、SQLLOADER数据导出导入的一点小总结

    1.SQLLOADER的CONTROL文件 //**************************************************************************** ...

  3. Zend Debugger 配置

    到官网 http://www.zend.com/en/products/studio/downloads 下载 windows 版 Studio Web Debugger 打开下载得到的压缩包,里面有 ...

  4. Asp.net Vnext api CORS( 跨域)

    概述 跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源.而这种访问是被同源策略所禁止的.CORS系统定义了一种浏览器和服务器交互的方式 ...

  5. prop

    用法:prop(属性|key,value|fn) 用例:点击全选/取消全选 // 全选 和全不选 $("#check_all").click(function () { if ($ ...

  6. JAVA类访问URL

    URI uri = new URI("https://www.baidu.com/");        Desktop desktop = null;        if (Des ...

  7. A Mathematical Curiosity 分类: HDU 2015-06-25 21:27 11人阅读 评论(0) 收藏

    A Mathematical Curiosity Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  8. 默认调用电脑IE版本最高版本

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  9. CSU 1114 平方根大搜索 java大数

    1114: 平方根大搜索 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 49  Solved: 23[Submit][Status][Web Board ...

  10. Codeforces Round #368 (Div. 2) A

    Description Small, but very brave, mouse Brain was not accepted to summer school of young villains. ...