UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description
An addition chain for n is an integer sequence with the following four properties:
- a0 = 1
- am = n
- a0<a1<a2<...<am-1<am
- For each k (
) there exist two (not neccessarily different) integers i and j (
) with ak =ai +aj
You are given an integer n. Your job is to construct an addition chain for n with minimal length. If there is more than one such sequence, any one is acceptable.
For example, <1,2,3,5> and <1,2,4,5> are both valid solutions when you are asked for an addition chain for 5.
Input Specification
The input file will contain one or more test cases. Each test case consists of one line containing one integer n ( ). Input is terminated by a value of zero (0) for n.
Output Specification
For each test case, print one line containing the required integer sequence. Separate the numbers by one blank.
Hint: The problem is a little time-critical, so use proper break conditions where necessary to reduce the search space.
Sample Input
5
7
12
15
77
0
Sample Output
1 2 4 5
1 2 4 6 7
1 2 4 8 12
1 2 4 5 10 15
1 2 4 8 9 17 34 68 77 大致题意:
给你个n,找从 0 到 n 的最短序列,序列满足 每一个 a[k] 都存在a[i]+a[j]=a[k],如果有很多,找到一个就够了。
解题思路:
用迭代加深搜索实现,需要注意很多地方剪枝。
#include <iostream>
#include <cstring>
using namespace std;
int n,ans[];
bool finish;
void dfs(int x,int deep){
if(finish) return ;
if(x==deep) { if(ans[x]==n)finish=; return; }
for(int i=;i<=x;i++){
for(int j=i;j<=x;j++) if(ans[i]+ans[j]>ans[x]&&ans[i]+ans[j]<=n){//剪枝
int sum=ans[i]+ans[j];
for(int k=x+;k<=deep;k++) sum<<=;//sum *= 2;当前为x; sum存于x+1;
if(sum<n) continue;//如果接下来一直是最大策略还是不能达到n,剪枝
ans[x+]=ans[i]+ans[j];
dfs(x+,deep);
if(finish) return ;
}
}
}
int main(){
while(scanf("%d",&n),n){
memset(ans,,sizeof(ans));
ans[finish=]=;
int tmp=n,deep=; while(tmp>>=) deep++;//求出最大深度;
while(!finish) dfs(,deep++);
cout<<ans[];
for(int i=;i<deep;i++) cout<<" "<<ans[i];
cout<<endl;
}return ;
}
UVA 529 - Addition Chains,迭代加深搜索+剪枝的更多相关文章
- [POJ2248] Addition Chains 迭代加深搜索
Addition Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5454 Accepted: 2923 ...
- [Vijos1308]埃及分数(迭代加深搜索 + 剪枝)
传送门 迭代加深搜索是必须的,先枚举加数个数 然后搜索分母 这里有一个强大的剪枝,就是确定分母的范围 #include <cstdio> #include <cstring> ...
- UVA 529 Addition Chains(迭代搜索)
Addition Chains An addition chain for n is an integer sequence with the following four propertie ...
- POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...
- POJ2248 Addition Chains 迭代加深
不知蓝书的标程在说什么,,,,于是自己想了一下...发现自己的代码短的一批... 限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索 #include<cstdio> ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains
此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...
- 【bzoj1085】【 [SCOI2005]骑士精神】启发式剪枝+迭代加深搜索
(上不了p站我要死了,侵权度娘背锅) 如果这就是启发式搜索的话,那启发式搜索也不是什么高级玩意嘛..(啪啪打脸) Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且 ...
- Power Calculus UVA - 1374 迭代加深搜索
迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...
随机推荐
- NSArray 数组操作
/*---------------------------创建数组------------------------------*/ //NSArray *array = [[NSArray alloc ...
- 查询sybase DB中占用空间最多的前20张表
按照数据行数查询 name, row_count(db_id(), id) from sysobjects order by row_count(db_id(),id) desc 按照分配的空间查询 ...
- ASP.NET和PHP全面对比
谁是速度之王? 刚刚在9月编程语言排行榜上取得历史性突破的PHP在Web开发领域最到的对手可能就是基于微软.NET技术的ASP.NET.近日,微软的 Joe Stagner在博客上发表了一系列文章比较 ...
- B - Numbers That Count
Description "Kronecker's Knumbers" is a little company that manufactures plastic di ...
- Java三大特征之多态(三)
面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...
- Eclipse代码风格设置
在编写代码的过程中,代码的呈现形式是通过eclipse的Formatter配置文件所控制的.我们可以按照自己的习惯生成属于自己的代码风格配置文件,方便规范以后的代码编写形式.具体的操作步骤如下所示:( ...
- oracle DBLink
现有两个oracle DB为A和B,为了能在A数据库中对B数据库进行操作,我们需要在A数据库中建立对B的DBLink. 在创建DBLink之前,我们首先去检查下DB A的global_names ...
- Berkeley DB基础教程
一.Berkeley DB的介绍 (1)Berkeley DB是一个嵌入式数据库,它适合于管理海量的.简单的数据.如Google使用其来保存账户信息,Heritrix用其来保存froniter. (2 ...
- 初始seajs
SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制 SeaJS本身遵循KISS(Keep It Simple, Stupid ...
- listview滚动到底部
方法一: // msgListView是ListView控件 // adapter是ListView绑定的Adapter,如果不方便直接使用,也可以通过ListView的getAdapter()方法获 ...