深度優先搜尋 DFS(Depath First Search, DFS)
深度優先搜尋是一種列舉所有完整路徑以遍歷所有情況的搜尋方法。(不撞南牆不回頭)
DFS一般用遞迴來實現,其虛擬碼思路過程一般如下:
void DFS(必要的引數){
if (符和遍歷到一條完整路徑的尾部){
更新某個全域性變數的值
}
if (跳出迴圈的臨界條件){
return;
}
對所有可能出現的情況進行遞迴
}
常見題型1:
程式碼實現:
1 #include <stdio.h> 2 const int maxn = 30; 3 int n, V, maxVal = 0; // 物品減數, 揹包容量,最大價值maxValue 4 int w[30]; 5 int c[30]; 6 int ans = 0; // 最大價值 7 8 // dfs, index是物品編號,nowW是當前所收納的物品容量,nowC是當前所收納的物品的總價值 9 void dfs(int index, int nowW, int nowC){ 10 if (index == n){ 11 return; 12 } 13 dfs(index + 1, nowW, nowC); // 不選第index件商品 14 if (nowW + w[index] <= V){ // 選第index件商品,但是先判斷容量是否超限 15 if (nowC + c[index] > ans){ 16 ans = nowC + c[index]; // 更新最大價值 17 } 18 dfs(index + 1, nowW + w[index], nowC + c[index]); 19 } 20 } 21 22 int main() 23 { 24 scanf("%d %d", &n, &V); 25 for (int i = 0; i < n; i++){ 26 scanf("%d", &w[i]); // 每件物品的重量 27 } 28 for (int i = 0; i < n; i++){ 29 scanf("%d", &c[i]); // 每件物品的價值 30 } 31 32 dfs(0, 0, 0); 33 printf("%d\n", ans); 34 35 return 0; 36 }
常見題型二:
列舉從N 個整數找那個選擇K個數(有時這個數可能可以重複)的所有方案(有時要列印這個方案的序列)
程式碼實現:
1 #include <stdio.h> 2 #include <vector> 3 using namespace std; 4 5 const int maxn = 30; 6 // 從包含n個數的序列A中選k個數使得和為x, 最大平方和為maxSumSqu; 7 int n, k, sum, maxSumSqu = -1, A[maxn]; 8 vector<int> temp, ans; // temp存放臨時方案,ans存放平方和最大的方案 9 10 void DFS(int index, int nowK, int nowSum, int nowSumSqu){ 11 if (nowK == k && nowSum == sum){ 12 if (nowSumSqu > maxSumSqu){ 13 maxSumSqu = nowSumSqu; 14 ans = temp; 15 } 16 return; 17 } 18 // 如果已經處理完n個數,或者選擇了超過k個數,或者和超過x 19 if (index == n && nowK > k && nowSum > sum){ 20 return; 21 } 22 23 // 選這個數 24 temp.push_back(A[index]); 25 DFS(index + 1, nowK + 1, nowSum + A[index], nowSumSqu + A[index] * A[index]); 26 27 // 不選這個數 28 // 先把剛加到temp中的資料去掉 29 temp.pop_back(); 30 DFS(index + 1, nowK, nowSum, nowSumSqu); 31 }
如果選出的k個數可以重複,那麼只需將上面“選這個數的 index + 1 改成 index 即可”
將
DFS(index + 1, nowK + 1, nowSum + A[index], nowSumSqu + A[index] * A[index]);
改成
DFS(index, nowK + 1, nowSum + A[index], nowSumSqu + A[index] * A[index]);
DFS題型實戰:
1103 Integer Factorization (30分)The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.
Input Specification:
Each input file contains one test case which gives in a line the three positive integers N (≤400), K (≤N) and P (1<P≤7). The numbers in a line are separated by a space.
Output Specification:
For each case, if the solution exists, output in the format:
N = n[1]^P + ... n[K]^P
where n[i]
(i
= 1, ..., K
) is the i
-th factor. All the factors must be printed in non-increasing order.
Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122+42+22+22+12, or 112+62+22+22+22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen -- sequence { a1,a2,⋯,aK } is said to be larger than { b1,b2,⋯,bK } if there exists 1≤L≤K such that ai=bi for i<L and aL>bL.
If there is no solution, simple output Impossible
.
Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossible
程式碼實現:
1 #include <stdio.h>
2 #include <vector>
3 #include <algorithm>
4 #include <math.h>
5 using namespace std;
6
7 // 從1 - 20中選出k個數,數可重複,使得這些數的p次方的和剛好等於n, 求這些序列中和最大的那個序列
8
9 int A[21];
10 int flag = 1;
11 int n, k, p, maxSum = -1;
12 vector<int> ans, temp, fac; // ans 存放最終序列, temp存放臨時序列
13
14 // 快速冪
15 int power(int i){
16 /*if (p == 1 )
17 return i;
18 if ((p & 1) != 0)
19 return i * power(i, p - 1);
20 else
21 {
22 int temp = power(i, p / 2);
23 return temp * temp;
24 }*/
25
26 int ans = 1;
27 for (int j = 0; j < p; j++){
28 ans *= i;
29 }
30 return ans;
31 }
32
33 // 求出所有不大於n的p次冪
34 void init(){
35 int i = 0, temp = 0;
36 while (temp <= n){
37 fac.push_back(temp);
38 temp = power(++i);
39 }
40 }
41
42 // DFS
43 void DFS(int index, int nowK, int sum, int squSum){
44 // 臨界條件
45 if (squSum == n && nowK == k){
46 if (sum > maxSum){
47 maxSum = sum;
48 ans = temp;
49 }
50
51 return;
52 }
53
54 if (sum > n || nowK > k){
55 return;
56 }
57
58 if (index >= 1){
59 // 遍歷所有可能的情況
60 // 選當前數
61 temp.push_back(index);
62 DFS(index, nowK + 1, sum + index, squSum + fac[index]);
63
64 // 不選當前數
65 temp.pop_back();
66 DFS(index - 1, nowK, sum, squSum);
67
68
69 }
70 }
71
72 int main()
73 {
74 // 讀取輸入
75 // freopen("in.txt", "r", stdin);
76 scanf("%d %d %d", &n, &k, &p);
77
78 // 初始化fac陣列
79 init();
80
81 // DFS尋找最合適的序列
82 DFS(fac.size() - 1, 0, 0, 0);
83
84 // 輸出
85 // 如果ans的size大於1則說明有結果
86 if (maxSum != -1){
87 // 排序
88 printf("%d = %d^%d", n, ans[0], p);
89 for (int i = 1; i < ans.size(); i++){
90 printf(" + %d^%d", ans[i], p);
91 }
92 }else
93 printf("Impossible");
94
95 // fclose(stdin);
96 return 0;
97 }
這個實戰題主要就是要先把 所有不超過 N 的 i ^p都算出來,要不然會超時