poj2362
#include<stdio.h> #include<string.h> #include<stdlib.h> int n,side;//num int stick[270]; bool vis[270]; int ok; int cmp(const void *a,const void *b){ return *(int *)b-*(int *)a; } void DFS(int num,int len,int s){ int i; if(num==3){ ok=1; return; } if(ok==1)return; for(i=s;i<n;i++){ if(vis[i]==true) continue; if(len+stick[i]<side){ vis[i]=true; DFS(num,len+stick[i],i); } else if(len+stick[i]==side){ vis[i]=true; DFS(num+1,0,0); } vis[i]=false; } } int main(){ int i,tp_sum; int time; scanf("%d",&time); while(time--){ scanf("%d",&n); tp_sum=0; for(i=0;i<n;i++){ scanf("%d",&stick[i]); tp_sum+=stick[i]; vis[i]=false; } side=tp_sum/4; qsort(stick,n,sizeof(stick[0]),cmp); if(stick[0]>side||tp_sum%4!=0){ printf("no\n"); continue; } ok=0; vis[0]=true; DFS(0,0,0);//当前得到的正方形为0,从第0个棒开始,该边的长度长度为0 if(ok==1){ printf("yes\n"); } else printf("no\n"); } return 0; }
难得DFS写出来了。。
keep moving...