Problem Descriptiongiven a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
Inputthe first line of input contains N, the number of test cases. Each test case is begins with an integer 4 <= M <=, the number of sticks. M integers follow; Each gives the length of a stick-an integer between 1 and 10,000.
Outputfor each case, the output a line containing "yes" if was is the possible to form a square; Otherwise output "no".
Sample Input34 1 1 1 15 10 20 30 40 508 1 7 2 6 4 4 3 5
Sample Outputyesnoyes//heard this is another classic search question ... All right.. is still the code of the great God to knock ...
#include <iostream>#include<cstring>using namespacestd;intT,n,sum,flag;inta[ -],visit[ -];voidDfsintBian,intLintk) { if(bian==5) {flag=1; return ; } if(l==sum) {DFS (Bian+1,0,0); if(flag)return ; } for(inti=k;i<n;i++) { if(!visit[i]&&a[i]+l<=sum) {Visit[i]=1; DFS (Bian,a[i]+l,i+1); if(flag)return ; Visit[i]=0; } } return ;}intMain () {intT; CIN>>T; while(t--) {cin>>N; Sum=0; for(intI=0; i<n;i++) {cin>>a[i]; sum+=A[i]; } if(sum%4!=0) {cout<<"No"<<Endl; Continue; } Sum=sum/4; intq=-1; for(intI=0; i<n;i++) { if(a[i]>sum) { //cout<< "#####" <<a[i]<< "" <<sum<<endl;q=i; Break; } } if(q!=-1) {cout<<"No"<<Endl; Continue; } memset (Visit,0,sizeof(visit)); Flag=0; DFS (1,0,0); if(flag) cout<<"Yes"<<Endl; Elsecout<<"No"<<Endl; } return 0;}
HDU 1518 Square