zoj_1909_dfs或hdu_1518

#include<iostream> #include<cstdio> #include<memory.h> using namespace std; int a[21]; int n; int visit[21]; int ave; int dfs(int cur,int count,int index)//index记下当前的a[index],如果没有index。则每次都要从头开始,肯定TLE { if(cur==ave) { count++; cur=0; index=0; //从头开始 if(count==3) return 1; } int i; for(i=index;i<n;i++) //从index开始遍历 if(!visit[i]&&cur+a[i]<=ave) { visit[i]=1; if(dfs(cur+a[i],count,i)) return 1; else visit[i]=0; } return 0; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(visit,0,sizeof(visit)); int i; int cur=0,count=0,sum=0,index=0; int max=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; if(max<a[i]) max=a[i]; } ave=sum/4; if(sum%4||max>ave) { printf("no/n"); continue; } if(dfs(cur,count,index)) printf("yes/n"); else printf("no/n"); } return 0; } 

你可能感兴趣的:(zoj_1909_dfs或hdu_1518)