您的位置:首页 > 编程语言 > Go语言

1518 square (DFS)

2014-07-01 18:23 429 查看
 http://acm.hdu.edu.cn/showproblem.php?pid=1518
DFS 枝剪
1.数量大于等于4
2.平均长度即正方形长度大于或等于最长的
3.数量被4整除

#include<stdio.h>

#include<algorithm>

#include<string>

using namespace std;

int cmp(int a,int b){

return a>b;

}

int len[21];

int loc[21];

int t;

int dfs(int d,int n,int w);

int sum;

int main()

{

int N;

scanf("%d",&N);

while(N--)

{

memset(len,0,sizeof(len));

memset(loc,0,sizeof(loc));

sum = 0;

int i;

scanf("%d",&t);

for(i=0;i<t;i++)

{

scanf("%d",&len[i]);

sum+= len[i];

}

sort(len,len+t,cmp);

if(sum%4||t<4||sum/4<len[0])

{

printf("no\n");

}else{

sum /= 4;

int f =dfs(0,0,0);

if(f)

printf("yes\n");

else

printf("no\n");

}

}

return 0;

}

int dfs(int d,int n,int w)

{

int i;

if(w==3)

return 1;

for(i=n;i<t;i++)

{

if(!loc[i])

{

loc[i] = 1;

if(d+len[i]<sum)

{

if(dfs(d+len[i],i+1,w))

return 1;

}else if(d+len[i]==sum)

{

if(dfs(0,0,w+1))

return 1;

}

loc[i] = 0;

}

}

return 0;

}

测试数据
    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<
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu algorithm dfs