标签:turn 临时 pop tar 可重复 str ber 快速幂 strong
深度优先搜索是一种枚举所有完整路径以遍历所有情况的搜索方法。(不撞南墙不回头)
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题型实战:
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.
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.
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 12?2??+4?2??+2?2??+2?2??+1?2??, or 11?2??+6?2??+2?2??+2?2??+2?2??, 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 { a?1??,a?2??,?,a?K?? } is said to be larger than { b?1??,b?2??,?,b?K?? } if there exists 1≤L≤K such that a?i??=b?i?? for i<L and a?L??>b?L??.
If there is no solution, simple output Impossible
.
169 5 2
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
169 167 3
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都算出来,要不然会超时
深度优先搜索 DFS(Depath First Search, DFS)
标签:turn 临时 pop tar 可重复 str ber 快速幂 strong
原文地址:https://www.cnblogs.com/hi3254014978/p/12236443.html