马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the edges of the polygon. We want to place a rotating scoreboard somewhere in the hall such that a spectator sitting anywhere on the boundary of the hall can view the scoreboard (i.e., his line of sight is not blocked by a wall). Note that if the line of sight of a spectator is tangent to the polygon boundary (either in a vertex or in an edge), he can still view the scoreboard. You may view spectator's seats as points along the boundary of the simple polygon, and consider the scoreboard as a point as well. Your program is given the corners of the hall (the vertices of the polygon), and must check if there is a location for the scoreboard (a point inside the polygon) such that the scoreboard can be viewed from any point on the edges of the polygon.
Input
The first number in the input line, T is the number of test cases. Each test case is specified on a single line of input in the form n x1 y1 x2 y2 ... xn yn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xi yi sequence specify the vertices of the polygon sorted in order.
Output
The output contains T lines, each corresponding to an input test case in that order. The output line contains either YES or NO depending on whether the scoreboard can be placed inside the hall conforming to the problem conditions.
Sample
Input:
- 2
- 4 0 0 0 1 1 1 1 0
- 8 0 0 0 2 1 2 1 1 2 1 2 2 3 2 3 0
复制代码 Output:
模板:- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- const int N = 110;
- const double eps = 1e-10;
- int sign(double x)
- {
- if (fabs(x) < eps) return 0;
- return x > 0 ? 1 : -1;
- }
- int dcmp(double x, double y)
- {
- if (fabs(x - y) < eps) return 0;
- return x > y ? 1 : -1;
- }
- int n;
- struct Point
- {
- double x, y;
- Point(double x = 0, double y = 0) :x(x), y(y) { }
- Point operator+ (Point t) { return Point(x + t.x, y + t.y); }
- Point operator- (Point t) { return Point(x - t.x, y - t.y); }
- Point operator* (double t) { return Point(x * t, y * t); }
- Point operator/ (double t) { return Point(x / t, y / t); }
- void read() { cin >> x >> y; }
- }P[N], ch[N];
- typedef Point Vector;
- struct Line
- {
- Point p, q;
- Vector v;
- double deg;
- Line() { }
- Line(Point p, Point q) : p(p), q(q) { v = q - p; deg = atan2(v.y, v.x); }
- }L[N], q[N];
- double cross(Vector a, Vector b)
- {
- return a.x * b.y - a.y * b.x;
- }
- double cross(Point A, Point B, Point C)
- {
- return cross(B - A, C - A);
- }
- double dot(Vector a, Vector b)
- {
- return a.x * b.x + a.y * b.y;
- }
- bool cmp(Line a, Line b)
- {
- if (!dcmp(a.deg, b.deg)) return cross(a.v, b.q - a.p) > 0;
- return a.deg < b.deg;
- }
- Point get_intersection(Point p, Vector v, Point q, Vector w)
- {
- Vector u = p - q;
- double t = cross(w, u) / cross(v, w);
- Point b = v * t;
- return p + b;
- }
- Point get_intersection(Line a, Line b)
- {
- return get_intersection(a.p, a.v, b.p, b.v);
- }
- //判断b, c的交点是否在a右边
- //等于 = 证明经过该点,就要删掉 = 的点(本题依照题意不能删除过重复点的点)
- bool on_right(Line a, Line b, Line c)
- {
- Point o = get_intersection(b, c);
- return sign(cross(a.p, a.q, o)) </*=*/ 0;
- }
- int half_plane_intersection(int n, Point ch[])
- {
- sort(L, L + n, cmp);
- int hh = 0, tt = 0, cnt = 0;
- for (int i = 0; i < n - 1; i++)
- {
- if (!dcmp(L[i].deg, L[i + 1].deg)) continue;
- L[cnt++] = L[i];
- }
- L[cnt++] = L[n - 1];
- for (int i = 0; i < cnt; i++)
- {
- while (hh + 1 < tt && on_right(L[i], q[tt - 1], q[tt - 2])) tt--;
- while (hh + 1 < tt && on_right(L[i], q[hh], q[hh + 1])) hh++;
- q[tt++] = L[i];
- }
- while (hh + 1 < tt && on_right(q[hh], q[tt - 1], q[tt - 2])) tt--;
- while (hh + 1 < tt && on_right(q[tt - 1], q[hh], q[hh + 1])) hh++;
- q[tt] = q[hh];
- cnt = 0;
- for (int i = hh; i < tt; i++)
- ch[cnt++] = get_intersection(q[i], q[i + 1]);
- return cnt;
- }
- bool check() //判断输入的点是否为逆时针
- {
- double ans = 0;
- for (int i = 1; i < n - 1; i++)
- {
- ans += cross(P[i] - P[0], P[i + 1] - P[0]);
- double a = cross(P[i] - P[0], P[i + 1] - P[0]);
- }
- return ans < 0;
- }
- int main()
- {
- int T;
- cin >> T;
- while (T--)
- {
- cin >> n;
- int m = 0;
- for (int i = 0; i < n; i++) P[i].read();
- if(check())
- for (int i = 0; i < n; i++) L[i] = Line(P[(i + 1) % n], P[i % n]);
- else
- for (int i = 0; i < n; i++) L[i] = Line(P[i % n], P[(i + 1) % n]);
- int cnt = half_plane_intersection(n, ch);
- if (cnt >= 3) puts("YES");
- else puts("NO");
- }
- return 0;
- }
复制代码 |