题目链接: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; ];…
[题目描述] 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 (1<=k<=m) there exist two (not necessarily different) integers i and j (0<=i, j<=k…
Addition Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5454   Accepted: 2923   Special Judge Description An addition chain for n is an integer sequence <a0, a1,a2,...,am="">with the following four properties: a0 = 1 a…
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 (1<=k<=m) there exist two (not necessarily different) integers i and j (0<=i, j&…
不知蓝书的标程在说什么,,,,于是自己想了一下...发现自己的代码短的一批... 限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索 #include<cstdio> #include<iostream> #include<cstring> #define R register int using namespace std; inline int g() { R ret=; register char ch; while(!isdigit(ch=…
原题 给出数n,求出1......n 一串数,其中每个数字分解的两个加数都在这个序列中(除了1,两个加数可以相同),要求这个序列最短. ++m,dfs得到即可.并且事实上不需要提前打好表,直接输出就可以. #include<cstdio> using namespace std; int dep=0,n; int a[102]; bool dfs(int step) { if(step>dep) return a[dep]==n; for(int i=0;i<step;i++) {…
Description 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.…
迭代加深dfs 每次控制序列的长度,依次加深搜索 有几个剪枝: 优化搜索顺序,从大往下枚举i, j这样能够让序列中的数尽快逼近n 对于不同i,j和可能是相等的,在枚举的时候用过的数肯定不会再被填上所以可以去重(记得回溯) #include <iostream> #include <cstring> #include <cstdio> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; inl…
链接 把迭代加深理解错了 自己写了半天也没写对 所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索.这种方法虽然会导致重复的遍历 某些结点,但是由于搜索的复杂度是呈指数级别增加的,所以对于下一层搜索,前面的工作可以忽略不计,因而不会导致时间上的亏空. IDA*就是一个加了层数限制depth的DFS,超过了限制就不在搜索下去,如果在当前层数没有搜到目标状态,就加大层数限制depth,这里还只是一个IDA算法,并不是A*的.当然我们可以用A*…
Description 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.…