-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClockwise712.cpp
More file actions
144 lines (144 loc) · 2.84 KB
/
Clockwise712.cpp
File metadata and controls
144 lines (144 loc) · 2.84 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
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
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <stack>
#include <algorithm>
using namespace std;
class Point {
public:
int x, y;
Point(int a, int b);
Point();
};
Point::Point(int a, int b) {
x = a;
y = b;
}
Point::Point() {
x = y = 0;
}
int determinant(Point p1, Point p2) {
return p1.x*p2.y - p2.x*p1.y;
}
double distance(Point p1, Point p2) {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
}
int AntiClockwise(Point p1, Point p2, Point p3) {
int val = determinant(p1, p2) + determinant(p2, p3) + determinant(p3, p1);
if (val > 0)
return 1;
else if (val < 0)
return -1;
return 0;
}
Point smallest;
int compare(const void* a, const void* b) {
Point* p1 = (Point*)a;
Point* p2 = (Point*)b;
int angle = AntiClockwise(smallest, *p1, *p2);
if (angle < 0)
return 1;
else if (angle > 0)
return -1;
else {
if (distance(smallest, *p2) > distance(smallest, *p1)) {
return -1;
}
else
return 1;
}
}
int compare1(const void* a, const void* b) {
Point* p1 = (Point*)a;
Point* p2 = (Point*)b;
int angle = AntiClockwise(smallest, *p1, *p2);
if (angle < 0)
return -1;
else if (angle > 0)
return 1;
else {
if (distance(smallest, *p2) > distance(smallest, *p1)) {
return -1;
}
else
return 1;
}
}
bool onSegment(Point p, Point q, Point r) {
if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) && q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
return true;
return false;
}
int main() {
Point points[300];
int n;
while (cin >> n) {
if (n == 0)
break;
for (int i = 0; i < n; i++)
cin >> points[i].x >> points[i].y;
int ctr = 0;
stack<Point> s;
s.push(points[0]);
s.push(points[1]);
for (int i = 2; i <n; i++) {
Point tmp = s.top();
s.pop();
Point tmp2 = s.top();
s.pop();
if (AntiClockwise(tmp, tmp2, points[i]) < 0) {// clockwise
ctr++;
s.push(tmp);
s.push(tmp2);
}
if (AntiClockwise(tmp, tmp2, points[i]) == 0 && !onSegment(tmp, points[i], tmp2)) {//angle is 0
ctr++;
s.push(tmp);
s.push(tmp2);
}
else {
s.push(tmp2);
s.push(points[i]);
}
}
s = stack<Point>();
s.push(points[0]);
s.push(points[1]);
int ctr1 = 0;
for (int i = 2; i < n; i++) {
Point tmp = s.top();
s.pop();
Point tmp2 = s.top();
s.pop();
if (AntiClockwise(tmp, tmp2, points[i]) > 0) {//anticlockwise
ctr1++;
s.push(tmp);
s.push(tmp2);
}
if (AntiClockwise(tmp, tmp2, points[i]) == 0 && onSegment(tmp, points[i], tmp2)) {// angle is 180
ctr1++;
s.push(tmp);
s.push(tmp2);
}
else {
s.push(tmp2);
s.push(points[i]);
}
}
if (ctr < ctr1) {
if (ctr == 0) {
cout << "CC" << endl;
}
else
cout << "Remove " << ctr << " bead(s), " << "CC" << endl;
}
else {
if (ctr1 == 0) {
cout << "C" << endl;
}
else
cout << "Remove " << ctr1 << " bead(s), " << "C" << endl;
}
}
return 0;
}