思路:
IDA*。
预先处理出1~1000的表,输入时再去查表。
估价函数:(Max2<<(maxd-x))<goal
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<sstream>
#include<queue>
#include<set>
using namespace std;
#define n 1000
int m;
int maxd;
int f[n+5]= {0};
int Abs(int x) {
if(x<0) return -x;
return x;
}
bool cmp(const int& a,const int& b) {
return a>b;
}
int vec[n*5]={0};
bool dfs(int goal,int x) {
if(vec[x]==goal) return true;
if(x==maxd) {
return false;
}
int Max1=0,Max2=0;
for(int i=0;i<=x;i++){
if(vec[i]>Max2){
Max1=Max2,Max2=vec[i];
}
}
if((Max2<<(maxd-x))<goal) return false;
for(int i=x; i>=0; i--) {
vec[x+1]=vec[i]+vec[x];
if(dfs(goal,x+1)) return true;
vec[x+1]=vec[x]-vec[i];
if(dfs(goal,x+1)) return true;
}
return false;
}
int main() {
for(int i=2; i<=n; i++) {
maxd=1;
for( ; maxd<13 ; maxd++) {
vec[0]=1;
if(dfs(i,0)) break;
}
f[i]=maxd;
}
while(scanf("%d",&m)&&m) {
printf("%d\n",f[m]);
}
return 0;
}