POJ 3134Power Calculus(IDA*)

Power Calculus
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1615   Accepted: 856

Description

Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:

x2 = x × xx3 = x2 × xx4 = x3 × x, …, x31 = x30 × x.

The operation of squaring can be appreciably shorten the sequence of multiplications. The following is a way to compute x31 with eight multiplications:

x2 = x × xx3 = x2 × xx6 = x3 × x3x7 = x6 × xx14 = x7 × x7x15 = x14 × xx30 = x15 × x15x31 = x30 × x.

This is not the shortest sequence of multiplications to compute x31. There are many ways with only seven multiplications. The following is one of them:

x2 = x × x, x4 = x2 × x2x8 = x4 × x4x8 = x4 × x4x10 = x8 × x2x20 = x10 × x10x30 = x20 × x10x31 = x30 × x.

If division is also available, we can find a even shorter sequence of operations. It is possible to compute x31 with six operations (five multiplications and one division):

x2 = x × xx4 = x2 × x2x8 = x4 × x4x16 = x8 × x8x32 = x16 × x16x31 = x32 ÷ x.

This is one of the most efficient ways to compute x31 if a division is as fast as a multiplication.

Your mission is to write a program to find the least number of operations to compute xn by multiplication and division starting with x for the given positive integer n. Products and quotients appearing in the sequence should be x to a positive integer’s power. In others words, x−3, for example, should never appear.

Input

The input is a sequence of one or more lines each containing a single integer nn is positive and less than or equal to 1000. The end of the input is indicated by a zero.

Output

Your program should print the least total number of multiplications and divisions required to compute xn starting with x for the integer n. The numbers should be written each in a separate line without any superfluous characters such as leading or trailing spaces.

Sample Input

1
31
70
91
473
512
811
953
0

Sample Output

0
6
8
9
11
9
13
12



第一道IDA*。。。


题目大意:求用一个x,如何在最小的步数使用已经用过的凑出x^n,其中n是1~1000的,可以搜索,很容易想到,<=0和>=2000的点应该直接剪掉。 
   
    开始一直在纠结BFS会不会超时,实在不行就打表,但是写到最后才发现自己写的BFS有问题。因为你每次扩充点的时候,如果选择不扩充,那么就必须回溯,因为这个点不能选择了。。。

      最后看了度娘的博客,写的IDA*,对那个理解也很浅显,就是搜索的时候控制搜索的层数即可。然后还有一个剪枝a[pos]<<(deep-pos)<n,这个点按最大比例每次都乘以2扩充也扩充不到n,那么就剪掉。


AC代码:
#include<iostream>
#include<cstdio>
using namespace std;

int a[2014];
int flag,deep,n;

void dfs(int pos)
{
    int t;
    if(flag||pos>deep) return;
    if(a[pos]<<(deep-pos)<n) return;   //这个点扩展最大扩展也扩展不到n,不用搜
    //必须要加这个剪枝
    if(a[pos]==n)
    {
        flag=1;
        return;
    }
    for(int i=1;i<=pos*2;i++)
    {
        if(flag) return;
        if(i<=pos)  t=a[pos]+a[i];
        else t=a[pos]-a[i-pos];
        if(t>=1 && t<2000)
        {
            a[pos+1]=t;
            dfs(pos+1);
        }
    }
}

int main()
{
    while(cin>>n&&n)
    {
        deep=1,flag=0;
        a[1]=1;
        while(1)
        {
            dfs(1);
            if(flag) break;
            deep++;
        }
        cout<<deep-1<<endl;
    }
    return 0;
}


你可能感兴趣的:(IDA)