-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.cc
178 lines (155 loc) · 3.87 KB
/
c.cc
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include<vector>
#include<set>
#include<algorithm>
#include<cstdio>
#include <cstdlib>
#include <ctime>
#define n 7
using namespace std;
int vx[n]; //vx,vy[i] is vector from i to i+1
int vy[n];
vector<int> x(n);
vector<int> y(n);
int maxarea = 0;
int minarea = n * n * 2;
int p[n];
int available[n];
int num_available = n;
int gt = 0;
//https://en.wikipedia.org/wiki/Shoelace_formula
int area() //computes 2*area
{
int r = 0;
for (int i = 0; i < n; i++) {
r += x[i] * (y[(i + 1) % n] - y[(i + n - 1) % n]);
}
return (r < 0) ? (0 - r) : r;
}
void pv(const vector<int> &vx, const vector<int> &vy)
{
for (int i = 0; i < n; i++) {
printf("(%d,%d),", 1 + vx[i], 1 + vy[i]);
}
printf("\n");
}
inline bool no_intersect(int a)
{
for (int i=0;i<a-1;i++){
int j = a-1;
int rs = vx[i] * vy[j] - vx[j] * vy[i];
if(!rs) return false;
}
for (int i = 0; i < a - 2; i++) {
int j = a - 1;
int rs = vx[i] * vy[j] - vx[j] * vy[i];
int t0 = (x[j] - x[i]) * vy[j] - (y[j] - y[i]) * vx[j];
int t1 = (x[j] - x[i]) * vy[i] - (y[j] - y[i]) * vx[i];
if (rs < 0) {
rs = 0 - rs;
t0 = 0 - t0;
t1 = 0 - t1;
}
if (t0 >= 0 && t0 <= rs && t1 >= 0 && t1 <= rs) {
return false;
}
}
if (a == n - 1) {
for (int i=0;i<n-1;i++){
int j = n-1;
int rs = vx[i] * vy[j] - vx[j] * vy[i];
if(!rs) return false;
}
for (int i = 1; i < n - 2; i++) {
int j = n - 1;
int rs = vx[i] * vy[j] - vx[j] * vy[i];
int t0 = (x[j] - x[i]) * vy[j] - (y[j] - y[i]) * vx[j];
int t1 = (x[j] - x[i]) * vy[i] - (y[j] - y[i]) * vx[i];
if (rs < 0) {
rs = 0 - rs;
t0 = 0 - t0;
t1 = 0 - t1;
}
if (t0 >= 0 && t0 <= rs && t1 >= 0 && t1 <= rs) {
return false;
}
}
}
return true;
}
bool EXPERIMENT(int a){
//answers true if the angles are acute/flat enough, to find minimal areas.
if(a>1){
int ps = vx[a-2]*vx[a-1]+vy[a-2]*vy[a-1];
ps = ps*ps;
int s1 = vx[a-1]*vx[a-1]+vy[a-1]*vy[a-1];
int s2 = vx[a-2]*vx[a-2]+vy[a-2]*vy[a-2];
float x = float(ps)/(float(s1)*float(s2));
return x>0.8;
}
return true;
}
void backtrack(int a)
{
gt++;
if (gt > 1e8) {
return;
}
if (a == n) {
int ar = area();
if (ar > maxarea) {
maxarea = ar;
printf("%d:\n", ar);
pv(x, y);
}
if (ar < minarea) {
minarea = ar;
printf("%d:\n", ar);
pv(x, y);
}
return;
}
for (int i = 0; i < num_available; ++i) {
int val = available[i];
y[a] = val;
if (a > 0) {
vx[a - 1] = x[a % n] - x[a - 1];
vy[a - 1] = y[a % n] - y[a - 1];
}
if (a == n - 1) {
vx[n - 1] = x[0] - x[n - 1];
vy[n - 1] = y[0] - y[n - 1];
}
if (!no_intersect(a) /*|| !EXPERIMENT(a)*/) {
continue;
}
//on peut mettre i en pos a
available[i] = available[num_available - 1];
available[num_available - 1] = val;
num_available--;
backtrack(a + 1);
num_available++;
available[num_available - 1] = available[i];
available[i] = val;
}
}
int main()
{
std::srand(std::time(0));
int t = 0;
for (int i = 0; i < n; i++) {
x[i] = y[i] = i;
}
do {
// random_shuffle(x.begin(), x.end()); //
backtrack(0);
gt = 0;
for (int i = 0; i < n; ++i) {
available[i] = i;
}
num_available = n;
t++;
//printf("%d/100\n", t);
// } while (t < 100000000);//
} while (next_permutation(x.begin(),x.end()) );//
return 0;
}