poj 3134 Power Calculus (IDA*)

貌似不加A*也能过。。。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int n,m,fl,dep;
 6 int tmp[1005];
 7 bool get_h(int x){
 8     int mxn=-1;
 9     if(dep-x>10)return true;
10     for(int i=1;i<=x;i++){
11         mxn=max(mxn,tmp[i]);
12     }
13     return (mxn*(1<<(dep-x+1))>=n);
14 }
15 bool dfs(int d){
16     if(d==dep+1){
17         if(tmp[d]==n)return true;
18         return false;
19     }
20     if(tmp[d]>n)return false;
21     if(!get_h(d))return false;
22     for(int i=d;i>=1;i--){
23         tmp[d+1]=tmp[d]+tmp[i];
24         if(dfs(d+1))return true;
25     }
26     for(int i=d;i>=1;i--){
27         if(tmp[d]>tmp[i])tmp[d+1]=tmp[d]-tmp[i];
28         if(dfs(d+1))return true;
29     }
30     return false;
31 }
32 int main(){
33     tmp[1]=1;
34     while(1){
35         scanf("%d",&n);
36         if(n==0)return 0;
37         for(dep=0;dep<=1000;dep++){
38             if(dfs(1))break;
39         }
40         printf("%d\n",dep);
41     }
42     return 0;
43 }

 

posted @ 2018-11-23 21:01  Mr_Handsome  阅读(97)  评论(0编辑  收藏  举报