poj 1011 搜索减枝
题目链接:http://poj.org/problem?id=1011
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int maxn = 100; int a[maxn]; int N; int sum; int len,cnt; bool used[maxn]; bool cmp(int a,int b){ return a > b; } bool dfs(int u,int curlen,int num){ if(num == cnt) return true; for(int i=u;i<=N;i++){ if(used[i]) continue; if(!used[i-1] && a[i-1] == a[i]) continue; if(curlen + a[i] == len){ used[i] = true; if(dfs(num+2,0,num+1)) return true; used[i] = false; return false; } else if(curlen + a[i] < len){ used[i] = true; if(dfs(i+1,curlen+a[i],num)) return true; used[i] = false; if(curlen == 0) return false; //一个大的都不能够找到答案,一个小的就更不可能了。 } } return false; } int main() { // freopen("E:\\acm\\input.txt","r",stdin); while(cin>>N && N){ sum = 0; for(int i=1;i<=N;i++){ scanf("%d",&a[i]); sum += a[i]; } sort(a+1,a+N+1,cmp); a[0] = -1; for(len=a[1];len<sum;len++){ if(sum % len) continue; cnt = sum / len; memset(used,0,sizeof(used)); if(dfs(1,0,0)) break; } printf("%d\n",len); } }