forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB2.cpp
More file actions
163 lines (136 loc) · 5.19 KB
/
B2.cpp
File metadata and controls
163 lines (136 loc) · 5.19 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int) (x).size())
using namespace std;
double safe_sqrt(double x) {
return sqrt(max((double)0.0,x));
}
int GI(ll& x) {
return scanf("%lld", &x);
}
const int MN = 511;
int n;
int a[MN][MN];
#define EPS 1e-6
const double PI = acos(-1.0);
double DEG_to_RAD(double d) { return d * PI / 180.0; }
double RAD_to_DEG(double r) { return r * 180.0 / PI; }
inline int cmp(double a, double b) {
return (a < b - EPS) ? -1 : ((a > b + EPS) ? 1 : 0);
}
struct Point { // <--> Vector
double x, y;
Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
Point operator + (const Point& a) const { return Point(x+a.x, y+a.y); }
Point operator - (const Point& a) const { return Point(x-a.x, y-a.y); }
Point operator * (double k) const { return Point(x*k, y*k); }
Point operator / (double k) const { return Point(x/k, y/k); }
bool operator<(const Point &rhs) const { return make_pair(y,x) < make_pair(rhs.y,rhs.x); }
bool operator==(const Point &rhs) const { return make_pair(y,x) == make_pair(rhs.y,rhs.x); }
double operator * (const Point& a) const { return x*a.x + y*a.y; } // dot product
double operator % (const Point& a) const { return x*a.y - y*a.x; } // cross product
int cmp(Point q) const { if (int t = ::cmp(x,q.x)) return t; return ::cmp(y,q.y); }
Point conj() { return Point(x, -y); }
double norm() { return x*x + y*y; }
// Note: There are 2 ways for implementing len():
// 1. sqrt(norm()) --> fast, but inaccurate (produce some values that are of order X^2)
// 2. hypot(x, y) --> slow, but much more accurate
double len() { return sqrt(norm()); }
Point rotate(double alpha) {
double cosa = cos(alpha), sina = sin(alpha);
return Point(x * cosa - y * sina, x * sina + y * cosa);
}
};
int ccw(Point a, Point b, Point c) {
return cmp((b-a)%(c-a),0);
}
typedef vector< Point > Polygon;
double area2(Point a, Point b, Point c) { return a%b + b%c + c%a; }
bool between(const Point &a, const Point &b, const Point &c) {
return (fabs(area2(a,b,c)) < EPS && (a.x-b.x)*(c.x-b.x) <= 0 && (a.y-b.y)*(c.y-b.y) <= 0);
}
void ConvexHull(vector<Point> &pts) {
sort(pts.begin(), pts.end());
pts.erase(unique(pts.begin(), pts.end()), pts.end());
vector<Point> up, dn;
for (int i = 0; i < pts.size(); i++) {
while (up.size() > 1 && area2(up[up.size()-2], up.back(), pts[i]) >= 0) up.pop_back();
while (dn.size() > 1 && area2(dn[dn.size()-2], dn.back(), pts[i]) <= 0) dn.pop_back();
up.push_back(pts[i]);
dn.push_back(pts[i]);
}
pts = dn;
for (int i = (int) up.size() - 2; i >= 1; i--) pts.push_back(up[i]);
if (pts.size() <= 2) return;
dn.clear();
dn.push_back(pts[0]);
dn.push_back(pts[1]);
for (int i = 2; i < pts.size(); i++) {
if (between(dn[dn.size()-2], dn[dn.size()-1], pts[i])) dn.pop_back();
dn.push_back(pts[i]);
}
if (dn.size() >= 3 && between(dn.back(), dn[0], dn[1])) {
dn[0] = dn.back();
dn.pop_back();
}
pts = dn;
}
bool check(int y, int x) {
if (!a[y][x]
|| !a[y+1][x]
|| !a[y][x+1]
|| !a[y+1][x+1]) return 0;
return 1;
}
istream& operator >> (istream& cin, Point& p) {
cin >> p.x >> p.y;
return cin;
}
ostream& operator << (ostream& cout, Point& p) {
cout << p.x << ' ' << p.y;
return cout;
}
#undef int
int main() {
#define int long long
ios :: sync_with_stdio(0); cin.tie(0);
cout << (fixed) << setprecision(9);
while (cin >> n && n) {
FORD(y,n,1) {
string s; cin >> s;
FOR(x,1,n) a[y][x] = s[x-1] - '0';
}
Polygon all;
FOR(y,1,n) FOR(x,1,n) if (a[y][x] == 1) {
if (check(y, x)) all.push_back(Point(x, y));
if (check(y-1, x)) all.push_back(Point(x, y-1));
if (check(y, x-1)) all.push_back(Point(x-1, y));
if (check(y-1, x-1)) all.push_back(Point(x-1, y-1));
}
ConvexHull(all);
if (ccw(all[0], all[1], all[2]) > 0) reverse(all.begin(), all.end());
vector< pair<int,int> > res;
for(auto p : all) {
res.push_back(make_pair(
(int) (round(p.x) + 1e-6),
(int) (round(p.y) + 1e-6)));
}
int start = 0;
FOR(i,1,SZ(res)-1)
if (res[i] < res[start]) start = i;
cout << SZ(res) << '\n';
REP(i,SZ(res)) {
int u = (i + start) % SZ(res);
cout << res[u].first << ' ' << res[u].second << '\n';
}
}
}