1317: Square(DFS+剪枝)

时间:2023-02-18 08:20:24

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".
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<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int data[250],state[250];
int ave,N,M,sum;
int dfs(int x,int pos,int len)
{
int i;
if(x==3)//如果有三条边都满足,结束
return 1;
for(i=pos;i>=0;i--){
if(!state[i]){
state[i]=1;
if(len+data[i]<ave){
if(dfs(x,i-1,len+data[i]))//i-1的作用是剪枝
return 1;
}
if(len+data[i]==ave){
if(dfs(x+1,M-1,0))
return 1;
}
state[i]=0;
}
}
return 0;
}
int main ()
{
scanf("%d",&N);
while(N--){
sum=0;
memset(state,0,sizeof(state));
//memset(data,0,sizeof(data));
scanf("%d",&M);
for(int i=0;i<M;sum+=data[i],i++)
scanf("%d",&data[i]);
ave=sum/4;
if(M<4||ave*4!=sum||ave<data[M-1]){
printf("no\n");
continue;
}
if(dfs(0,M-1,0))
printf("yes\n");
else
printf("no\n"); }
return 0;
}

1317: Square(DFS+剪枝)的更多相关文章

  1. HDU1518 Square&lpar;DFS&comma;剪枝是关键呀&rpar;

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  2. 【DFS&plus;剪枝】Square

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/J [题意] 给定n个木棍,问这些木棍能否围成一个正方形 [Accepted] # ...

  3. POJ 3009 DFS&plus;剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  4. &ast;HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. poj 1724&colon;ROADS(DFS &plus; 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  6. DFS&lpar;剪枝&rpar; POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  7. DFS&plus;剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  8. HDU 5952 Counting Cliques 【DFS&plus;剪枝】 (2016ACM&sol;ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 5937 Equation 【DFS&plus;剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. SQL Injection bypass WAF

    tips: 利用的注射点: 支持Union 可报错 支持多行执行.可执行系统命令.可HTTP Request等额外有利条件 若非以上类型,则可能需要暴力猜解.猜解时,可能会遇到一些限制.攻击者要做的, ...

  2. Shell学习笔记 - 环境变量配置文件&lpar;转&rpar;

    一.source命令 功能:在当前bash环境下读取并执行配置文件中的命令 1. 命令格式 source 配置文件  或  . 配置文件 2. 命令示例 [root@localhost ~]# sou ...

  3. 实战使用Axure设计App&comma;使用WebStorm开发&lpar;4&rpar; – 实现页面UI

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  4. 好脑袋不如烂笔头-Quartz使用总结

    Quartz是Java平台的一个开源的作业调度框架.Quartz.net是从java版本移植到.net版本的..net项目使用Quartz来执行批处理等定时任务非常方便. (1)从nuget上可以安装 ...

  5. Thinkphp源码分析系列–开篇

    目前国内比较流行的php框架由thinkphp,yii,Zend Framework,CodeIgniter等.一直觉得自己在php方面还是一个小学生,只会用别人的框架,自己也没有写过,当然不是自己不 ...

  6. WPF绑定方式

    绑定到其它元素 <Grid>     <StackPanel>         <TextBox x:Name="textbox1" />    ...

  7. Storm累计求和进群运行代码

    打成jar包放在主节点上去运行. import java.util.Map; import backtype.storm.Config; import backtype.storm.StormSubm ...

  8. What is the best way to handle Invalid CSRF token found in the request when session times out in Spring security

    18.5.1 Timeouts One issue is that the expected CSRF token is stored in the HttpSession, so as soon a ...

  9. SharePoint 前端开发常用的对象之&lowbar;spPageContextInfo

    前言 _spPageContextInfo对象,是SharePoint开发一个非常常用的对象,尤其是前端开发,可以非常方便的获取到一些和站点有关的信息. 完整对象如下图,需要什么属性,可以自己获取,然 ...

  10. 如何添加使用echats地图悬浮显示内容

    /初始化绘制全国地图配置 var option = { backgroundColor: '#000', title: { text: 'Echarts3 中国地图农村金融', subtext: '三 ...