POJ 3134 - Power Calculus 迭代加深搜索(DFSID)

     最开始想动态规划...想了好久想不通.然后试着写BFS..各种超时....参考大牛的提示..DFSID把它A掉了...效率很高啊...而且代码写起来思路也很清晰...    


Program:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#define ll long long
#define oo 10000007
using namespace std;     
int way[1005],num; 
bool DFSID(int x,int step)
{ 
      int i;
      if (num>step) return false;
      if (way[num]==x) return true;
      if (way[num]<<(step-num)  < x) return false; // 强力剪枝 
      for (i=0;i<=num;i++)
      {
             num++;
             way[num]=way[num-1]+way[i];
             if (way[num]<=10000 && DFSID(x,step)) return true; 
             way[num]=way[num-1]-way[i];  
             if (way[num]>0 && DFSID(x,step)) return true;           
             num--;
      } 
      return false;    
}
int main()
{  
      int i,x;
      freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);      
      while (~scanf("%d",&x) && x) 
      {
            for (i=0;;i++)
            {
                 way[num=0]=1;
                 if (DFSID(x,i)) break;
            }
            printf("%d\n",i);
      }
      return 0;
}


你可能感兴趣的:(POJ 3134 - Power Calculus 迭代加深搜索(DFSID))