Article Directory
Iterative deepening search
What is iterative deepening search?
Iterative deepening search (Iterative Deepening DFS, IDDFS) search method is a combination of DFS and BFS thought. When the search tree deep and wide, when, with DFS will fall into a recursive unable to return, with the BFS queue space will explode, you can try IDDFS, simply put, is DFS search k layer, if feasible solution is not found immediately returned then DFS search k + 1 layer, until you find a feasible solution, using BFS idea on the number of layers to gradually expand the search depth of DFS.
IDA *
valuation function to optimize the iterative deepening search, that optimistic estimate pruning. When you find the least number of layers + current levels> limit the number of layers required solution, exit.
example
Portal: 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
The meaning of problems
Given the number of x and n, x seek the n- , only with multiplication and division, counted the results can be utilized, at least asked count how many times?
analysis
1 starts from the digital equivalent, with the addition and subtraction, minimum n-count how many times to give?
, And previously generated values were obtained by calculating the new value of the acceleration current value, it is determined whether or equal to n.
Valuation function: If the current value is the fastest way (even multiplied by 2) n can not reach the exit.
Code
#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("%d\n", k);
}
return 0;
}
summary
- Space complexity and the same deep search, time search complexity is somewhat inferior to wide (in fact, the front layer k-1 for the complexity of the waste can be ignored).
- Suitable for enough time, less space, there is no clear upper limit of the depth of the search topic.
- Pruning !!!
Your thumbs will be my greatest motivation