poj2362 Square(DFS)
题目链接
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)的更多相关文章
- HDU 1518 Square(DFS)
Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...
- HDU1518 Square(DFS)
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 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 ...
- Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)
Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
随机推荐
- 手脱FSG v1.33
1.载入PEID FSG v1.33 (Eng) -> dulek/xt 2.载入OD,先F8跟一会 004103E3 > BE A4014000 mov esi,fsg1_33.0040 ...
- array_intersect
<?php date_default_timezone_set('Asia/Shanghai'); $a1=array("a"=>"red",&qu ...
- 阿里云ECS/Ubuntu下***浅析
公司项目中需要WebRTC作为即时通讯部分的核心技术,这部分的开发由我负责.实际上手前需要访问谷歌进行源码的下载以及编译,在这里记录下我各种折腾服务器***过程. 目前手上有两台阿里云ESC: 华南节 ...
- NOIP模拟赛13
期望得分:100+0+100=200 实际得分:100+5+100=205 T1 空间卡到30M.. n<=2.5*1e7 若x是整除区间[1,n]每个数的最小的数,那么对[1,n]每个数分解质 ...
- 2-sat 分类讨论 UVALIVE 3713
蓝书326 //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Redis-1-Redis的安装
Redis 什么是Redis? redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value数据库. 安装Redis: windows下如何安装? 官方网址:ht ...
- 51nod 1161 Partial Sums
给出一个数组A,经过一次处理,生成一个数组S,数组S中的每个值相当于数组A的累加,比如:A = {1 3 5 6} => S = {1 4 9 15}.如果对生成的数组S再进行一次累加操作,{1 ...
- 数据库-SQLite
技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong 数据库-SQLite 技术博客http:// ...
- filezilla显示隐藏文件
我们在习惯使用flashfxp等工具,但是随着主机商限制较多,这些老的FTP工具不怎么好用了,比如主机商会推荐使用到Filezilla等工具.但是比如息壤主机,我们在管理linux环境下htacess ...