-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcircle_array.cpp
More file actions
69 lines (63 loc) · 1.53 KB
/
Copy pathcircle_array.cpp
File metadata and controls
69 lines (63 loc) · 1.53 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
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int max1 = 10000;
double min1 = 1000;
double X[max1], r[max1];// 分别为每个圆心的横坐标 ,每个圆的半径 ,
double small[max1]; // 最小圆排列的半径顺序
int n;
double center(int t) {
double temp = 0;
for (int i = 1; i < t; ++i) {
double res = X[i] + 2.0 * sqrt(r[t] * r[i]); //. 目标圆T有可能能够和排在它之前的任意一个圆相切,因此需要以一判断取最大值
temp = max(temp, res);
}
return temp;
}
void len() {
double min2 = 0, max2 = 0; // 计算最优圆排列的最左端 和 最右端
for (int i = 1; i <= n ; ++i) {
if (X[i] - r[i] < min2) min2 = X[i] - r[i];
if (X[i] + r[i] > max2) max2 = X[i] + r[i];
}
if (max2 - min2 < min1) {
min1 = max2 - min2; // 更新最小圆排列
for (int i = 1; i <= n ; ++i) {
small[i] = r[i];
}
}
}
void dfs(int t) {
if (t == n + 1) {
len();
} else {
for (int i = t; i <= n; ++i) {
swap(r[t], r[i]);//构造全排列
double X_t = center(t);
if (X_t + r[t] + r[1] < min1) {
X[t] = X_t;
dfs(t + 1);
}
swap(r[t], r[i]); //恢复现场
}
}
}
int main() {
cout << "please enter the numbers of circle:";
cin >> n;
cout << "please enter the circle:";
for (int i = 1; i <= n; ++i) {
cin >> r[i];
}
for (int i = 1; i <= n; ++i) {
cout <<"the "<< i << " cirlce :" << r[i] << endl;
}
dfs(1);
cout << "the length is:" << min1 << endl;
cout << "the circle list:";
for (int i = 1; i <= n; ++i) {
cout << small[i] << " ";
}
return 0;
}