迭代加深搜索
什麼是迭代加深搜索?
迭代加深搜索(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("%d\n", k);
}
return 0;
}
小結
- 空間複雜度相對較小(其實前面k-1層的浪費對於複雜度來說可以忽略),如果廣/深搜爆棧可以一試。
- 適用於時間充裕,空間較小,沒有明確搜索深度上限的題。
- 剪枝!!!
你的點贊將會是我最大的動力