迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)

Posted sapphirebitter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)相关的知识,希望对你有一定的参考价值。

本题代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int step,n;
int pow[1000];

bool dfs(int d,int maxd)///d即是所走步数
{
    if(pow[d]==n)
    {
        printf("%d\n",d);
        return true;
    }
    if(d==maxd) return false;
    int maxv = pow[0];
    for(int i = 1; i <= d; i++) maxv = max(maxv, pow[i]);
    if((maxv << (maxd-d)) < n) return false;///当其中最大的值乘以2的maxd-d次方以后仍然小于n,则剪枝
    for(int i=d; i>=0; i--)///重复数据对本题无影响,因为每次进行maxd的增大时,pow数组就相当于重新由第二位开始赋值,与先前相同的幂数也不会对结果产生影响,只是会增加所消耗的时间
    {
        pow[d+1]=pow[d]+pow[i];///先走加法再走减法
        if(dfs(d+1,maxd)) return true;
        pow[d+1]=pow[d]-pow[i];
        if(dfs(d+1,maxd)) return true;
    }
    return false;
}

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        if(n==1)
        {
            printf("0\n");
            continue;
        }
        pow[0]=1;
        for(int maxd=1;; maxd++)
        {
            if(dfs(0,maxd))
                break;
        }
    }
    return 0;
}

 

以上是关于迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)的主要内容,如果未能解决你的问题,请参考以下文章

Power Calculus UVA - 1374 迭代加深搜索

UVA1374-Power Calculus(迭代加深搜索)

POJ-3134-Power Calculus(迭代加深DFS)

POJ 3134 - Power Calculus (IDDFS)

POJ 3134 Power Calculus (迭代剪枝搜索)

POJ 3134 - Power Calculus