本题代码如下:

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

迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)的更多相关文章

  1. 【算法•日更•第三十九期】迭代加深搜索:洛谷SP7579 YOKOF - Power Calculus 题解

    废话不多说,直接上题: SP7579 YOKOF - Power Calculus 题意翻译 (略过没有营养的题干) 题目大意: 给出正整数n,若只能使用乘法或除法,输出使x经过运算(自己乘或除自己, ...

  2. UVA-1374 Power Calculus (迭代加深搜索)

    题目大意:问最少经过几次乘除法可以使x变成xn. 题目分析:迭代加深搜索. 代码如下: # include<iostream> # include<cstdio> # incl ...

  3. Power Calculus UVA - 1374 迭代加深搜索

    迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...

  4. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  5. poj 2248 Addition Chains (迭代加深搜索)

    [题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...

  6. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  7. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  8. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  9. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  10. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

随机推荐

  1. api资源

    转:https://blog.csdn.net/qq_37187976/article/details/79160050

  2. Ubuntu16.10下使用VSCode开发.netcore

    按照通常的套路,首先创建一个空白的解决方案,需要用到.netcore sdk命令: dotnet new sln -o dotnetcore_tutrorial 这个时候可以看到在目标目录下生成了一个 ...

  3. day05列表 类型

    基本使用 1用途:记录多个值,比如人的多个爱好 # ======================================基本使用================================ ...

  4. datetime.strptime格式转换报错ValueError

    今天遇到一个报错:ValueError: time data '2018-10-10(Wednesday) AM0:50' does not match format '%Y-%m-%d(%A) %p ...

  5. python 学习 map /reduce

    python 内建了map()和reduce()函数 map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. ...

  6. asp.net MVC之Action过滤器浅析

    在asp.net MVC中,Action过滤器是一大利器,它可以在以下两个步骤执行相关的代码: 1.执行Action方法之前:OnActionExecuting 2.Action方法执行完毕后:OnA ...

  7. Cracking The Coding Interview 1.7

    //Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set ...

  8. 开发中最好使用not exists 取代not in

    开发中使用not in可能会存在致命的错误,在子查询中,如果存在空值,not in返回的数据就是空了,如下创建2张数据表: user表: 部门表: 现在要查询没有分配到用户的部门有哪些,使用not i ...

  9. 使用MyEclipse开发Java EE应用:用XDoclet创建EJB 2 Session Bean项目(一)

    [MyEclipse最新版下载] 一.创建一个EJB项目 1. 选择File>New Project,选择EJB Project,然后单击Next. 2. 在Project name字段中输入f ...

  10. Ubuntu16.04 安装Teamviewer

    有时需要远程控制ubuntu系统的电脑,Teamviewer在linux下也可以进行安装,大致看了下向日葵在linux下配置好像比较麻烦,而且Teamviewer远程控制的流畅性一直不错,就选择安装T ...