HDOJ1518Square 深搜

Square
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11099 Accepted Submission(s): 3566

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
题意就是:看这个数组中的数字组合是否能够构成一个正方形
不能分割数字,不能重复组合

代码:

1#include<string.h> 2#include<stdio.h> 3#include<algorithm> 4using namespace std; 5int n,s,su,a[1010],vis[1010],len; 6bool cmp(int a,int b) 7{ 8 return a>b;//从大到小排序 9} 10void dfs(int a1,int a2,int a3)//(0,0,1) 11{ 12 if(a1==3)/**只需要筹齐3次,那么剩下的一定能够成len长度**/ 13 { 14 su=1; 15 return ; 16 } 17 if(su==1) 18 return ;/**优化时间**/ 19 for(int i=a3; i<=n; i++) 20 { 21 /**对于那些用过的和不符合条件的,for那里可以不扫,故从a3开始**/ 22 /**a3前面的对于最初的a2来说一定不符合**/ 23 if(vis[i]==0) 24 { 25 vis[i]=1; 26 if(a2+a[i]==len) 27 { 28 dfs(a1+1,0,1); 29 /**但是换另外一条边的时候a3要改回1,因为那些未用的,对上一条边来说不符合条件的,可能符合这条边的条件**/ 30 } 31 else if(a2+a[i]<len) 32 { 33 dfs(a1,a2+a[i],i+1); 34 /**没筹齐从i+1继续,前面的不符合**/ 35 while(a[i]==a[i+1]) 36 i++; 37 //回溯后如果后面的相同那么不需要再DFS 38 //前面的数和后面的相同就可以跳过这个数,剪枝 39 } 40 vis[i]=0; 41 } 42 } 43} 44 45int main() 46{ 47 int i; 48 scanf("%d",&s); 49 while(s--) 50 { 51 su=0; 52 len=0; 53 memset(vis,0,sizeof(vis)); 54 //memset函数在string.h头文件中 55 scanf("%d",&n); 56 for(i=1; i<=n; i++) 57 { 58 scanf("%d",&a[i]); 59 len=len+a[i]; 60 } 61 int mm=len/4; 62 sort(a+1,a+n+1,cmp); 63 //在algorithm头文件中 64 if(len%4==0&&a[1]<=mm&&n>=4) 65 { 66 len/=4; 67 dfs(0,0,1); 68 if(su==1) 69 printf("yes\n"); 70 else 71 printf("no\n"); 72 } 73 else 74 { 75 printf("no\n"); 76 } 77 } 78 return 0; 79}

本文同步分享在 博客“谙忆”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java 复制Map对象(深拷贝与浅拷贝)

java复制Map对象(深拷贝与浅拷贝)CreationTime2018年6月4日10点00分Author:Marydon1.深拷贝与浅拷贝  浅拷贝:只复制对象的引用,两个引用仍然指向同一个对象

输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。

题目内容:输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。例:(1)输入:Ilovehebeu!输出:character:10,space:2,digit:0,others:1(2)输入:2020,haveabrilliantyear!输出:character:18,space:4,digit:4,others:2答案:c

Typescript 常见的几种函数重载方法详解与应用示例

所谓的重载,其实就是使用相同的函数名,传入不同数量的参数或不同类型的参数,以此创建出多个方法或产生不同结果。1\.最常见的,也就是根据定义傻瓜式地判断参数类型与数量functionshowPerson(name,...others){console.log(name,others)}