迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)
本题代码如下:
#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为例)的更多相关文章
- 【算法•日更•第三十九期】迭代加深搜索:洛谷SP7579 YOKOF - Power Calculus 题解
废话不多说,直接上题: SP7579 YOKOF - Power Calculus 题意翻译 (略过没有营养的题干) 题目大意: 给出正整数n,若只能使用乘法或除法,输出使x经过运算(自己乘或除自己, ...
- UVA-1374 Power Calculus (迭代加深搜索)
题目大意:问最少经过几次乘除法可以使x变成xn. 题目分析:迭代加深搜索. 代码如下: # include<iostream> # include<cstdio> # incl ...
- Power Calculus UVA - 1374 迭代加深搜索
迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
随机推荐
- python3.7 安装
python3.7 安装 下载安装 cd /usr/localwget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgztar -xvf ...
- api资源
转:https://blog.csdn.net/qq_37187976/article/details/79160050
- GitHub下的文件放到Linux系统下
1.在GitHub账号下clone URL 项目. 2.到Linux服务器下执行以下操作: (1) mkdir test (2) cd test/ (3) git clone 复制的项目URL
- vue2整个项目中,数据请求显示loading图
一般项目中,有时候会要求,你在数据请求的时候显示一张gif图片,然后数据加载完后,消失.这个,一般只需要在封装的axios中写入js事件即可.当然,我们首先需要在app.vue中,加入此图片.如下: ...
- bzoj1238
题解: 傻逼模拟题 果断的复制了题解(还没有c++题解?) 代码: program p2509; type arr=array[..] of boolean; var tot:longint; s:a ...
- linux-安装jdk以及tomcat
1.安装jdk 下载地址:www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260.html 将jdk下 ...
- dns资源记录类型
资源记录的定义格式: 语法:name [TTL] IN RR_TYPE value SOA: name:当前区域的名字,例如"magedu.com.",或者"2.168. ...
- Android : App通过LocalSocket 与 HAL间通信
LocalSocket其通信方式与Socket差不多,只是LocalSocket没有跨越网络边界.对于*nix系统来说,“一切皆为文件”,Socket也不例外,Socket按照收发双方的媒介来说有三种 ...
- centos7创建docker tomcat镜像
1 准备宿主系统 准备一个 CentOS 7操作系统,具体要求如下: 必须是 64 位操作系统 建议内核在 3.8 以上 通过以下命令查看您的 CentOS 内核: 1 # uname -r 2 安装 ...
- Iscloc用法笔记
一. Iscloc的安装(使用Docker CE) 1.install Docker CE: https://www.digitalocean.com/community/tutorials/how- ...