题目链接

http://poj.org/problem?id=2362

题意

输入n根棍子的长度,求这n根棍子是否能组成一个正方形。

思路

假设能组成正方形,则正方形的周长为sum,sum/4为正方形的边长,问题转化为这n根棍子能否组成4根长度为side的棍子。由于棍子的长度越长,组合的灵活性就越差,所以将n根棍子按从长到短排序后dfs。

代码

 #include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int N = ;
vector<int> stick;
int visit[N];
int n;
int side; bool cmp(int a, int b)
{
return a>b;
} bool dfs(int num, int len, int cur)
{
if(num==)
return true; for(int i=cur; i<n; i++)
{
if(visit[i])
continue;
visit[i] = ;
if(len+stick[i]==side)
{
if(dfs(num+, , ))
return true;
}
else if(len+stick[i]<side)
{
if(dfs(num, len+stick[i], i+))
return true;
}
visit[i] = ;
}
return false;
} int main()
{
//freopen("poj2362.txt", "r", stdin);
int t;
cin>>t;
while(t--)
{
int sum = ;
stick.clear();
cin>>n;
for(int i=; i<n; i++)
{
int len;
cin>>len;
sum += len;
stick.push_back(len);
}
side = sum / ;
sort(stick.begin(), stick.end(), cmp);
if(sum%!= || side<stick[])
{
cout<<"no"<<endl;
continue;
}
memset(visit, , sizeof(visit));
bool ans = dfs(, , );
if(ans)
cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return ;
}

相似题目

1、poj1011:该题的增强版。

参考

1、http://blog.csdn.net/xindoo/article/details/8867919

poj2362 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. HDU1518 Square(DFS)

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

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

  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. [DeeplearningAI笔记]卷积神经网络2.9-2.10迁移学习与数据增强

    4.2深度卷积网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.9迁移学习 迁移学习的基础知识已经介绍过,本篇博文将介绍提高的部分. 提高迁移学习的速度 可以将迁移学习模型冻结的部分看 ...

  2. vim 常用快捷键(整理版)

    最常用: x 删除后面的字符 X 删除前一个字符  删除3个字符就是3x dd:删除一行   D 删除到行尾 J:删除换行符,使下一行并上来.     nJ:连接后面的n行 u:撤销上一次操作     ...

  3. Codeforces 221 B. Little Elephant and Numbers

    B. Little Elephant and Numbers time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  4. [转]C/C++作用域详解

    原文地址:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777433.html 作用域规则告诉我们一个变量的有效范围,它在哪儿创建,在 ...

  5. 01 DIV+CSS 固定页面布局

    本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. DIV+CSS布局中主要CSS属性介绍: Float: Float属性是DIV+CSS布局中最基本也是 ...

  6. Nodejs文件监控chokidar

    最近有个需求是扫描用例,用例是放在svn上,如果每次扫描都去遍历目录的话会有占用太多的io,所以想着用文件监控,有文件变化时只对该文件进行操作. Nodejs里的 chokidar 模块可以更好的对文 ...

  7. JQuery对RadioButton和CheckButton的操作

    js对RadioButton和CheckButton的操作,在网站开发中会经常遇到,而JQuery操作RadioButton和CheckButton非常便捷.小编觉得网站开发人员有必要熟练掌握.所以小 ...

  8. 在C++11中实现监听者模式

    参考文章:https://coderwall.com/p/u4w9ra/implementing-signals-in-c-11 最近在完成C++大作业时,碰到了监听者模式的需求. 尽管C++下也可以 ...

  9. jQuery()方法的第二个参数详解

    关于jQuery()方法的第二个参数,有下面这几种用法: 1.jQuery(selector, [context]) 这种用法,相当于 $(context).find(selector) 或者 con ...

  10. 面向过程编程(OPP) 和面向对象编程(OOP)的关系

    面向过程编程(OPP) 和面向对象编程(OOP)的关系 原文链接:http://blog.csdn.net/phphot/article/details/3985480 关于面向过程的编程(OPP)和 ...