DFS
题意是让你把这些木棍组合成长度相等的一些木棍。要求长度最短。
#include还有更强大的剪枝版本号的:#include #include #include using namespace std;int a[65],n,sum,ans;bool v[65],ok;bool cmpa(int a,int b){ return a>b;}bool dfs(int num,int need,int total){ int i; if(need==0) { if(total==sum/ans-2)return 1; for(i=0;v[i];i++); v[i]=1; if(dfs(i+1,ans-a[i],total+1))return 1; v[i]=0; return 0; } else { if(num>=n-1)return 0; for(i=num;i
#include#include #include #define MAX 64int s[MAX],vis[MAX],n,len,sum;void input(){ int i,j,temp; sum = 0; for(i=0;i len) continue; if(now_len+s[k]==len) { vis[k]=1; bool result = dfs(0,0,count+1); vis[k]=0; return result; } else if(now_len==0) { vis[k]=1; bool result = dfs(s[k],k+1,count); vis[k]= 0; return result; } else if(now_len+s[k]