本题代码如下:

#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. eclipse安装springboot插件

    1.工具栏->Help->Eclise Marketplace打开应用市场 2.在应用市场中搜索sts,选择Spring Tools 4紧张安装 3.按提示进行安装

  2. elasticsearch在CentOS环境下开机启动

    验证环境,OS版本:CentOS-7-x86_64-Minimal-1708:ES版本:elasticsearch-6.2.2. 1.创建文件elasticsearch #!/bin/bash # # ...

  3. vue 项目中的坑 在项目中遇到 持续更新ing

    1.vue2.0 不支持 v-html 后绑定的内容使用过滤,可是有时候过滤必须使用-----------解决:通过methods中定义方法 然后 v-html='myMethods(string)' ...

  4. Generative Model 与 Discriminative Model

      [摘要]    - 生成模型(Generative Model) :无穷样本==>概率密度模型 = 产生模型==>预测    - 判别模型(Discriminative Model): ...

  5. composer install Your requirements could not be resolved to an installable set of packages

    composer install --ignore-platform-reqs 或者 composer update --ignore-platform-reqs

  6. LY.JAVA面向对象编程思想概述

    面向对象 2018年7月5日  逆袭之旅DAY09 2018年7月5日  逆袭之旅DAY09 2018-07-07

  7. java实现按中文首字母排序的方式

    public class ABD { public static void main(String[] args) { //Collator类是用来执行区分语言环境的String比较的,这里是选择CH ...

  8. Date和 Calendar

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...

  9. gulp安装和使用

    1.全局安装gulp:sudo npm install -g gulp 2.代码根目录:npm install 3.gulp 开始编译(在项目根目录下创建一个名为 gulpfile.js 的文件) 注 ...

  10. vue-router-1-安装与基本使用

    npm install vue-router import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) / ...