POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248
题解:
迭代加深DFS。
DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放入 $x[p+1]$。
迭代加深思路:设定一个深度限制,一旦到达这个界限,即继续往下搜索;该深度限制从 $1$ 开始,每次自加 $1$。这么做的好处是,正好也符合题目要求的最短的数组长度。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n,x[]; int d;
bool dfs(int p)
{
if(p==d) return x[p]==n;
bool vis[];
memset(vis,,sizeof(vis));
for(int i=p;i>=;i--)
{
for(int j=p;j>=i;j--)
{
int tp=x[i]+x[j];
if(tp>n || x[p]>=tp) continue;
if(!vis[tp])
{
x[p+]=tp;
if(dfs(p+)) return ;
else vis[tp]=;
}
}
}
return ;
} int main()
{
x[]=;
while(cin>>n && n)
{
for(d=;!dfs();d++);
for(int i=;i<=d;i++) printf("%d ",x[i]);
printf("\n");
}
}
POJ 2248 - Addition Chains - [迭代加深DFS]的更多相关文章
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- [POJ2248] Addition Chains 迭代加深搜索
Addition Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5454 Accepted: 2923 ...
- [POJ 2248]Addition Chains
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- POJ2248 Addition Chains 迭代加深
不知蓝书的标程在说什么,,,,于是自己想了一下...发现自己的代码短的一批... 限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索 #include<cstdio> ...
- [zoj] 1937 [poj] 2248 Addition Chains || ID-DFS
原题 给出数n,求出1......n 一串数,其中每个数字分解的两个加数都在这个序列中(除了1,两个加数可以相同),要求这个序列最短. ++m,dfs得到即可.并且事实上不需要提前打好表,直接输出就可 ...
- poj 3134 Power Calculus(迭代加深dfs+强剪枝)
Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...
- POJ 2245 Addition Chains(算竞进阶习题)
迭代加深dfs 每次控制序列的长度,依次加深搜索 有几个剪枝: 优化搜索顺序,从大往下枚举i, j这样能够让序列中的数尽快逼近n 对于不同i,j和可能是相等的,在枚举的时候用过的数肯定不会再被填上所以 ...
- poj2286The Rotation Game(迭代加深dfs)
链接 把迭代加深理解错了 自己写了半天也没写对 所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索.这种方法虽然会导致重复的遍历 某些结点,但 ...
- POJ-3134-Power Calculus(迭代加深DFS)
Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...
随机推荐
- reStructuredText语法简单说明
reStructuredText 是扩展名为.rst的纯文本文件,含义为"重新构建的文本"",也被简称为:RST或reST. 官方网址: http://docutils. ...
- oralce 11.2.0.4手动创建EM
安装完oracle,启动dbconsole,失败 [oracle@elearning admin]$ emctl start dbconsole OC4J Configuration issue. / ...
- Charles配置问题
1. 手机访问chls.pro/ssl下载证书时候,用常用安卓手机不同的浏览器(可以多试几种浏览器) 会出现两种情况,一种是直接打开下载getssl.crt文件 一种是没有反应,直接打开网页了 这时候 ...
- logstash之filter处理中括号包围的内容
如题,logstash之filter处理中括号包围的内容: $grep -v "#" config/logstash-nlp.yml input { kafka { bootstr ...
- hdoj:2049
#include <iostream> using namespace std; ]; /* n 个 数中 m个错排 转化为:充n个数中选取m个数,共有C(n,m)中,选取的m个数进行全部 ...
- Spring中@Resource与@Autowired、@Qualifier的用法与区别
1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...
- Houdini技术体系大纲
Houdini for UE4 Pipeline的系列教程,前言等想好再写吧
- 【2019年03月29日】股票的滚动市盈率PE最低排名
仅根据最新的市盈率计算公式进行排名,无法对未来的业绩做出预测. 深康佳A(SZ000016) - 滚动市盈率PE:2.51 - 滚动市净率PB:1.68 - 滚动年化股息收益率:2.9% - - - ...
- SQLSERVER查询那个表里有数据
declare @table table (rows int,tablename nvarchar(100));declare @sql NVARCHAR(MAX)declare @rows int; ...
- 删除JS 对象属性(元素)
var a={"id":1,"name":"danlis"}; //添加属性 a.age=18; console.log(a); //结果: ...