poj 2362 Square

 
Square
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 22315 Accepted: 7780

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

 

 

好久没写题目了,想搞个DFS练练手,打错一个字母T了一晚上,给跪了........还是太弱了

这题就暴力DFS,剪枝不困难

最显然就是总长度一定要是4的倍数,还有就是如果最长木棍大于正方形边长,直接输出NO

 

1.木棍降序排,因为越长的木棍肯定要先放好

2.如果搜到一半发现已经组成了3条边,那么一定是yes.因为总长度是4的倍数,剩下的一条边一定可以构造出来

 

 

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;

int n;
int len[110],totallen;
int side;
bool visit[110];

int cmp (int a,int b)
{
    return a>b;
}

bool dfs (int done,int now,int nowlen)
{
     if (done==3) return 1;
     for (int i=now;i<=n;i++)
     {
         if (!visit[i])
         {
            if (nowlen+len[i]==side)
             {
               visit[i]=1;
               if (dfs (done+1,1,0)) return 1;
               visit[i]=0;
             }
             if (nowlen+len[i]<side)
             {
               visit[i]=1;
               if (dfs (done,i+1,nowlen+len[i])) return 1;
               visit[i]=0;
             }
         }
     }
     return 0;
}

void doing ()
{
     cin>>n;
     totallen=0;
     for (int i=1;i<=n;i++)
     {
       cin>>len[i];
       totallen+=len[i];
     }
     side=totallen/4;
     if ((totallen%4!=0)||(len[1]>side)||(n<4))
     {
       cout<<"no"<<endl;
       return ;
     }
     sort (len+1,len+1+n,cmp);
     memset (visit,0,sizeof(visit));
     if (dfs(0,1,0))
       cout<<"yes"<<endl;
     else
       cout<<"no"<<endl;     
}

int main ()
{
    int T;
    cin>>T;
    while (T--)
      doing ();
    system ("pause");
    return 0;
}

 

 

 

 

 

 

 

//另外附上DISCUSS中不错的数据一组


10
20 491 1050 1123 376 1045 1391 656 190 1305 537 20 533 1193 465 398 1258 756
1005 949 63
20 1431 949 852 616 899 1257 413 335 528 107 1126 146 782 571 407 114 1393
681 1229 1340
20 715 321 723 362 778 156 402 25 145 414 571 835 198 393 289 851 390 234
1288 450
20 1450 1289 710 1209 287 202 1394 732 480 1121 1246 286 146 1333 1467 926
471 453 921 889
20 407 1269 625 577 184 617 1357 994 107 250 783 117 1352 1355 820 16 336
147 697 1134
20 679 1102 1502 1321 374 1472 288 428 1210 1497 1244 1412 573 1224 654 1465
528 891 1417 311
20 1063 872 1391 987 513 566 1258 831 1295 604 1512 549 365 712 1146 1133 89
561 1065 1476
20 1515 1473 1033 159 312 502 1342 1097 1231 476 1419 737 1200 174 1486 1311
1261 416 1109 43
20 930 1165 584 56 1122 858 955 564 932 375 1267 580 1515 553 423 467 605
722 1183 792
20 1035 1338 77 1253 243 715 177 951 852 1409 736 1170 694 945 555 289 1410
539 869 1059
no
yes
yes
yes
yes
no
no
yes
no
yes
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值