/*
一开始想到了简单的深搜 维护当前可用的mi数组
然后回溯用哪个 不断更新新产生的mi
这样的问题是 由于mi不断产生 搜索规模扩大
不好 不好 下面是奇丑的WA掉的代码 做个反面教材
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,ans=0x3f3f3f3f,f[],s[],top,vis[];
void Dfs(int p,int c)
{
if(p<=||p>n*)return;
if(f[p]==)s[++top]=p,f[p]=;
if(p==n){ans=min(ans,c);return;}
if(c>ans)return;
for(int i=;i<=top;i++)
if(vis[i]==)
{
vis[i]=;
Dfs(p+s[i],c+);
Dfs(p-s[i],c+);
vis[i]=;
}
}
int main()
{
scanf("%d",&n);
ans=n;
Dfs(,);
printf("%d\n",ans);
return ;
}
/*
正解是迭代加深
对于每次的搜索 我们限制最多能做几次运算
这样搜索的规模就大大减小
同样的维护已经得到的mi数组
数组的大小对应做了几次运算
加上几个剪枝:
如果mi中最大的<<(limit-k)都到不了n 搜索失败
生成新的mi的时候 尽量组合数大的 这样也可以减小规模
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 20
using namespace std;
int n,a[N*];
bool Dfs(int k,int limit)
{
if(a[k]==n)return ;
if(k==limit)return ;
int maxx=;
for(int i=;i<=k;i++)maxx=max(maxx,a[k]);
if(maxx<<(limit-k)<n)return ;
for(int i=k;i>=;i--)
{
a[k+]=a[i]+a[k];
if(Dfs(k+,limit))return ;
a[k+]=a[k]-a[i];
if(Dfs(k+,limit))return ;
}
return ;
}
int find()
{
if(n==)return ;
a[]=;
for(int i=;i<=N;i++)
if(Dfs(,i))return i;
}
int main()
{
scanf("%d",&n);
printf("%d\n",find());
return ;
}

codevs 2541 幂运算(迭代加深搜索)的更多相关文章

  1. codevs 2541 幂运算(迭代加深搜索)

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

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

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

  3. codevs 1288 埃及分数 (迭代加深搜索)

    题目大意:给你一个分数$a/b$,把它拆解成$\sum_{i=1}^{n}1/ai$的形式,必须保证$ai$互不相同的情况下,尽量保证n最小,其次保证分母最大的分数的分母最小 什么鬼玄学题!!! 因为 ...

  4. Codevs 四子连棋 (迭代加深搜索)

    题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双 ...

  5. 埃及分数问题_迭代加深搜索_C++

    一.题目背景 http://codevs.cn/problem/1288/ 给出一个真分数,求用最少的1/a形式的分数表示出这个真分数,在数量相同的情况下保证最小的分数最大,且每个分数不同. 如 19 ...

  6. 迭代加深搜索[codevs1004 四子连棋]

    迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...

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

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

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

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

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

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

随机推荐

  1. [Usaco2006 Dec]Milk Patterns

    [Usaco2006 Dec]Milk Patterns Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天 产奶的质量,但连续的若干天的 ...

  2. Laravel框架——自己写的类找不到

    composer.json my model files are stored in directory of app\models, therefor "autoload": { ...

  3. [BZOJ 2004] [Hnoi2010] Bus 公交线路 【状压DP + 矩阵乘法】

    题目链接: BZOJ - 2004 题目分析 看到题目完全不会..于是立即看神犇们的题解. 由于 p<=10 ,所以想到是使用状压.将每个连续的 p 个位置压缩成一个 p 位 2 进制数,其中共 ...

  4. Android 判断wifi和4G网络是否开启

    public boolean isWifiAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) g ...

  5. python operator模块

    官网:https://docs.python.org/3.4/library/operator.html http://pymotw.com/2/operator/

  6. Ehcache中配置详解

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...

  7. 14.5.2.2 autocommit, Commit, and Rollback

    14.5.2.2 autocommit, Commit, and Rollback 在InnoDB,所有的用户活动发生在一个事务里, 如果自动提交模式是启用的, 每个SQL语句形成一个单独的事务.默认 ...

  8. 调用系统api修改系统时间

    一:截图 二:代码 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  9. kafka-简单事例

    开始创建项目,这里所用的工程结构是maven. 在pox.xml中添加kafka的依赖包,如下所示: <dependency> <groupId>org.apache.kafk ...

  10. HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

    Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...