HDU1518 Square
#include<cstdio> #include<cstring> #include<iostream> using namespace std; int L,n; int l[21]; bool vis[21]; int dfs(int nused,int left,int pos){ if(nused==0&&left==0) return 1; if(left==0)left=L; for(int i=pos;i<n;i++){ if(!vis[i]&&left>=l[i]){ if(i>0&&!vis[i-1]&&l[i]==l[i-1]) continue; vis[i]=1; if(left==l[i]){ if(dfs(nused-1,left-l[i],0)) return 1; else{ vis[i]=0; return 0; } } else{ if(dfs(nused-1,left-l[i],i+1)) return 1; else vis[i]=0; } } } return 0; } int main() { int i,N,sum; scanf("%d",&N); while(N--){ memset(vis,0,sizeof(vis)); scanf("%d",&n); for(sum=i=0;i<n;i++){ scanf("%d",&l[i]); sum+=l[i]; } if(sum%4!=0){ printf("no\n"); continue; } L=sum/4; sort(l,l+n,greater<int >()); if(l[0]>L){ printf("no\n"); continue; } if(dfs(n,L,0)) printf("yes\n"); else printf("no\n"); } return 0; }