博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1518(DFS+剪枝)
阅读量:4314 次
发布时间:2019-06-06

本文共 1530 字,大约阅读时间需要 5 分钟。

Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 13374    Accepted Submission(s): 4244

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
 

 

Source
 
题意:问n根木棍能否组成一个正方形?
题解:排序+暴力+剪枝。。那些15ms的大神是怎么做到的。。
#include 
#include
#include
#include
#include
using namespace std;int v[25];int n;int sum = 0;bool vis[25];bool flag;//cnt代表当前到第几根了,len代表当前木棍长度void dfs(int cnt,int len,int now){ if(cnt==4){ flag = true; return; } for(int i=now;i
len+v[i]){ vis[i] = true; dfs(cnt,len+v[i],i+1); vis[i] = false; } }}int cmp(int a,int b){ return a>b;}int main(){ int tcase; scanf("%d",&tcase); while(tcase--){ scanf("%d",&n); sum = 0; for(int i=0;i
sum) { printf("no\n"); continue; } memset(vis,false,sizeof(vis)); flag = false; dfs(0,0,0); if(flag) printf("yes\n"); else printf("no\n"); }}

 

转载于:https://www.cnblogs.com/liyinggang/p/5740427.html

你可能感兴趣的文章
ES6 Iterator
查看>>
Apache2.4开启GZIP功能
查看>>
远程桌面关闭重启电脑的方法
查看>>
第三章 熟悉常用的HDFS操作
查看>>
filter:expression(document.execCommand("BackgroundImageCache",false,true) 转
查看>>
Java - 30 Java 网络编程
查看>>
shiro中的filterChainDefinitions
查看>>
瑞柏匡丞教你如何和程序员一起愉快的玩耍
查看>>
【单调队列】Vijos P1771 瑞士轮 (NOIP2011普及组第三题)
查看>>
【模拟】NEERC15 E Easy Problemset (2015-2016 ACM-ICPC)(Codeforces GYM 100851)
查看>>
JavaBean and PreparedStatement Usage
查看>>
经典冒泡排序
查看>>
HDU1312:Red and Black(DFS)
查看>>
es6 async与await实战
查看>>
北京行——xml解析之SAX
查看>>
Oracle_创建和管理表
查看>>
Retry Pattern
查看>>
字符串反转---指针
查看>>
SyntaxError: keyword can't be an expression解决方法
查看>>
高级特性(2)-迭代
查看>>