-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_DFS.cpp
More file actions
71 lines (64 loc) · 1.67 KB
/
8_DFS.cpp
File metadata and controls
71 lines (64 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* P273 DFS 选取平方和最大的方案
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 序列 A 中 n 个数,选取 k 个数使得和为 x,同时最大平方和为 maxSquareSum
int n;
int k;
int x;
int maxSquareSum = -1;
int *array = new int[n + 1];
// temp 存放临时方案,result 存放平方和最大方案
vector<int> temp;
vector<int> result;
/**
* index: 当前要处理 index 号整数
* currentK: 当前已选整数 K
* currentSum: 当前整数和
* currentSquareSum: 当前整数平方和
*/
void DFS(int index, int currentK, int currentSum, int currentSquareSum)
{
if (currentK == k && currentSum == x)
{
if (currentSquareSum > maxSquareSum)
{
maxSquareSum = currentSquareSum;
// 更新最优方案
result = temp;
}
}
// 已经处理完 n 个数,或者超过 k 个数,或者和超过 x
if (index >= n || currentK > k || currentSum > x)
{
return;
}
// 选 index 号整数
temp.push_back(array[index]);
DFS(index + 1, currentK + 1, currentSum + array[index], currentSquareSum + array[index] * array[index]);
temp.pop_back();
// 不选 index 号整数
DFS(index + 1, currentK, currentSum, currentSquareSum);
}
int main()
{
cout << "Input n, k, x: ";
cin >> n >> k >> x;
cout << "Input array(n numbers): " << endl;
for (int i = 0; i < n; i++)
{
cin >> array[i];
}
DFS(0, 0, 0, 0);
// print result
for (vector<int>::iterator it = result.begin(); it != result.end(); it++)
{
cout << *it << " ";
}
cout << endl;
delete [] array;
return 0;
}