标签:
关键在于优化剪枝
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int mapp[20+5],visit[20+5]; int flag; int l,m; void dfs(int s,int x,int num) { visit[s]=1; if(x==l) num++,s=0,x=0; if(num==4) { flag=1;return; } if(flag) return; for(int i=s+1;i<m;i++)//每次都从0开始跑会超时 { if(!visit[i]&&x+mapp[i]<=l) dfs(i,x+mapp[i],num),visit[i]=0; } } int main() { int t; scanf("%d",&t); while(t--) { cin>>m; int sum=0; for(int i=0;i<m;i++) scanf("%d",&mapp[i]),sum+=mapp[i]; sort(mapp,mapp+m); if(sum%4!=0||m<4||mapp[m-1]>sum/4) { cout<<"no"<<endl; continue; } l=sum/4; memset(visit,0,sizeof(visit)); flag=0; dfs(0,mapp[0],0); if(flag) cout<<"yes"; else cout<<"no"; cout<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/44928447