迭代加深搜索-POJ 3134 Power Calculus
文章目录
- 迭代加深搜索
- 例题
- 题意
- 分析
- 代码
- 小结
迭代加深搜索
什么是迭代加深搜索? 迭代加深搜索(Iterative Deepening DFS,IDDFS)是一种结合了DFS和BFS思想的搜索方法。当搜索树很深且很宽的时候,用DFS会陷入递归无法返回,用BFS队列空间会爆炸,那么可以试试IDDFS,简单来说,就是每次限制搜索深度的DFS。比如DFS搜索k层,若没有找到可行解则立即返回,再DFS搜索k+1层,直到找到可行解为止,在层数上采用BFS思想来逐步扩大DFS的搜索深度。
IDA* 估价函数对迭代加深搜索的优化,即乐观估计剪枝。当找到解需要的至少层数+当前层数>层数限制时,直接退出。
例题
传送门: POJ-3134
Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x. The operation of squaring can be appreciably shorten the sequence of multiplications. The following is a way to compute x31 with eight multiplications: x2 = x × x, x3 = x2 × x, x6 = x3 × x3, x7 = x6 × x, x14 = x7 × x7, x15 = x14 × x, x30 = x15 × x15, x31 = x30 × x. This is not the shortest sequence of multiplications to compute x31. There are many ways with only seven multiplications. The following is one of them: x2 = x × x, x4 = x2 × x2, x8 = x4 × x4, x8 = x4 × x4, x10 = x8 × x2, x20 = x10 × x10, x30 = x20 × x10, x31 = x30 × x. If division is also available, we can find a even shorter sequence of operations. It is possible to compute x31 with six operations (five multiplications and one division): x2 = x × x, x4 = x2 × x2, x8 = x4 × x4, x16 = x8 × x8, x32 = x16 × x16, x31 = x32 ÷ x. This is one of the most efficient ways to compute x31 if a division is as fast as a multiplication. Your mission is to write a program to find the least number of operations to compute xn by multiplication and division starting with x for the given positive integer n. Products and quotients appearing in the sequence should be x to a positive integer’s power. In others words, x−3, for example, should never appear.
input:
The input is a sequence of one or more lines each containing a single integer n. n is positive and less than or equal to 1000. The end of the input is indicated by a zero.
output:
Your program should print the least total number of multiplications and divisions required to compute xn starting with x for the integer n. The numbers should be written each in a separate line without any superfluous characters such as leading or trailing spaces.
Sample Input:
1
31
70
91
473
512
811
953
0
Sample Output:
0
6
8
9
11
9
13
12
题意
给定数x和n,求xn ,只能用乘法和除法,算过的结果可以被利用,问最少算多少次?
分析
等价于从数字1开始,用加减法,最少算多少次得到n? 用当前值和之前产生的值进行加减运算得到新的值,判断是否等于n。 估价函数:如果当前值用最快的方式(连乘2倍增)都不能到达n则退出。
代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int maxn = 1010;
int a[maxn];
int n, k;
bool ida(int now,int curk) {
if (curk > k)return true;//当前层数大于层数限制
//剩下的层数(层数限制-当前层)用最乐观的倍增也不能达到n
if (now << (k - curk) < n)return true;
return false;
}
bool iddfs(int now, int curk) {
if (ida(now, curk))return false;
if (now == n)return true;
a[curk] = now;
for (int i = 0; i <= curk; i++) {//遍历之前算过的值
//加
if (iddfs(now + a[i], curk + 1))return true;
//减
else if (iddfs(abs(now - a[i]), curk + 1))return true;
}
return false;
}
int main() {
while (~scanf("%d", &n) && n) {
for (k = 0;; k++) {//每次最大搜索k层
memset(a, 0, sizeof(a));
if (iddfs(1, 0))break;//从数字1开始,当前层0
}
printf("%dn", k);
}
return 0;
}
小结
- 空间复杂度相对较小(其实前面k-1层的浪费对于复杂度来说可以忽略),如果广/深搜爆栈可以一试。
- 适用于时间充裕,空间较小,没有明确搜索深度上限的题。
- 剪枝!!!
原创不易,请勿转载(
本不富裕的访问量雪上加霜) 博主首页:https://blog.csdn.net/qq_45034708
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 你不知道的Golang map
- go 1.11 模块和版本管理
- Golang Testing单元测试指南
- 你不知道的Go unsafe.Pointer uintptr原理和玩法
- 深度剖析各种BloomFilter的原理、改进、应用场景
- 深入理解哈希表
- iota: Golang 中优雅的常量
- Go template高级用法、深入详解、手册、指南、剖析
- Docker开启Remote API 访问 2375端口
- Docker实用debug调试技巧锦集
- shell 命名管道,进程间通信, ncat作http server
- Linux atop监控
- go modules中使用replace用法
- 分布式文件系统Ceph的挂载方式
- 在CentOS 7上挂载CephFS