hdoj--1518--Square(dfs)
Square
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5
yes
no
yes
#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)的更多相关文章
- HDU 1518 Square(DFS)
Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...
- HDOJ.1342 Lotto (DFS)
Lotto [从零开始DFS(0)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tempter of ...
- hdoj 1518 Square 【dfs】
题意:给出n个(不同长度的)棍子,问能不能将他们构成一个正方形. 策略:深搜. hdoj 1455的简化版 代码: #include <stdio.h> #include <stri ...
- 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 ...
- poj2362 Square(DFS)
题目链接 http://poj.org/problem?id=2362 题意 输入n根棍子的长度,求这n根棍子是否能组成一个正方形. 思路 假设能组成正方形,则正方形的周长为sum,sum/4为正方形 ...
- 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 在 ...
随机推荐
- Springboot使用JdbcTemplate RowMapper查询,直接返回实体列表
ManagerRowMapper.java public class ManagerRowMapper implements RowMapper<Manager>{ @Override p ...
- node.js(API解读) - process (http://snoopyxdy.blog.163.com/blog/static/60117440201192841649337/)
node.js(API解读) - process 2011-10-28 17:05:34| 分类: node | 标签:nodejs nodejsprocess node.jsprocess ...
- java aop面向切面编程
最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...
- 企业级mysql数据库完全备份、增量备份脚本
企业完全备份脚本 [root@client ~]# vim /opt/mysql_bak_wanbei.sh #!/bin/bash #MySQL数据库完全备份脚本 #设置登录变量 MY_USER=& ...
- MHA的MySQL高可用方案实战
功能: 1)master的故障切换(keepalived VIP的飘移) 2)主从复制角色的提升和重新转向 其中master 对外提供写服务,备选master2(实际的slave提供读服务,slave ...
- modelsim 仿真软件 百度云分享 modelsim se 10.7 10.6d 10.6c 10.5 10.4
modelsim se 10.7 链接:https://pan.baidu.com/s/1NDC2yMCZmA4bIRSk2dUiTg 提取码:4l1d 复制这段内容后打开百度网盘手机App,操作更方 ...
- 如何区分null和undefined
null和undefined是两种数据类型, 如果硬要区分的话. null是一种类型, 赋值变量为null型. 未定义的变量, 即为undefined. var a = null a // null ...
- Linux之iptables(二、基本认识和组成)
iptables的基本认识 Netfilter组件 内核空间,集成在linux内核中 扩展各种网络服务的结构化底层框架 内核中选取五个位置放了五个hook(勾子) function(INPUT.OUT ...
- namespace的作用及用法
namespace 所谓namespace,是指标识符的可见范围.C++标准库中的所有标识符都被定义在一个名为 std 的namespace 中. 一.<iostream>和<ios ...
- 第八节:pandas字符串
Pandas提供了一组字符串函数,可以方便地对字符串数据进行操作.