POJ 3134 Power Calculus 迭代加深搜索

来源:互联网 发布:linux mysql dump文件 编辑:程序博客网 时间:2024/05/18 03:51
第一发迭代加深搜索,参考网上的。。。
#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int ans, n;int a[15];bool dfs(int dep, int x){    if(a[dep] == n)        return true;    if(dep == ans)        return false;    x = max(x, a[dep]);    if(x*(1<<(ans-dep)) < n)        return false;    for(int i = 0; i <= dep; i++)    {        a[dep+1] = a[i]+a[dep];        if(dfs(dep+1, x))            return true;        if(a[i] > a[dep])            a[dep+1] = a[i]-a[dep];        else            a[dep+1] = a[dep]-a[i];        if(dfs(dep+1, x))            return true;    }    return false;}int main(){    while(scanf("%d", &n) != EOF)    {        if(n == 0)            break;        if(n == 1)            printf("0\n");        else        {            a[0] = 1;            for(ans = 1; ; ans++)            {                if(dfs(0, 1))                {                    printf("%d\n", ans);                    break;                }            }        }    }    return 0;}
0 0
原创粉丝点击