HDU 1518 Square

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.