POJ - Rotating Scoreboard(半平面交)
题目链接:http://poj.org/problem?id=3335
Time Limit: 2000MS Memory Limit: 65536K
Description
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 ynwhere 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
Sample Output
YES
NO
Problem solving report:
Description: 判断多边形是否有内核。
Problem solving: 直接上半平面交板子。
Accepted Code:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 105;
const double eps = 1e-7;
typedef struct point {
double x, y;
}vect;
struct line {
vect p;
point u, v;
bool operator < (const line& s) const {
return atan2(s.p.y, s.p.x) > atan2(p.y, p.x);
}
};
vect operator - (const point a, const point b) {
return (vect){a.x - b.x, a.y - b.y};
}
double dist(const point a, const point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int pnz(double x) {
return x < -eps ? -1 : x > eps ? 1 : 0;
}
double Cross(const vect a, const vect b) {
return a.x * b.y - a.y * b.x;
}
int Onleft(const point p, const line l) {
return pnz(Cross(l.p, p - l.u));
}
point Inter(const line a, const line b) {
double t = Cross(b.p, a.u - b.u) / Cross(a.p, b.p);
return (point){a.u.x + t * a.p.x, a.u.y + t * a.p.y};
}
bool Halfplane(line Sline[], int n) {
point Spt[MAXN];
int Q[MAXN] = {0};
int l = 0, r = 0;
sort(Sline, Sline + n);
for (int i = 1; i < n; i++) {
while (l < r && Onleft(Spt[r - 1], Sline[i]) < 0)
r--;
while (l < r && Onleft(Spt[l], Sline[i]) < 0)
l++;
Q[++r] = i;
if (!pnz(Cross(Sline[i].p, Sline[Q[r - 1]].p))) {
r--;
if (Onleft(Sline[i].u, Sline[Q[r]]) > 0)
Q[r] = i;
}
else Spt[r - 1] = Inter(Sline[i], Sline[Q[r - 1]]);
}
while (l < r && Onleft(Spt[r - 1], Sline[Q[l]]) < 0)
r--;
while (l < r && Onleft(Spt[l], Sline[Q[r]]) < 0)
l++;
if (r - l < 2)
return false;
return true;
}
int main() {
int n, t;
point p[MAXN];
line Sl[MAXN], Sline[MAXN];
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = n - 1; i >= 0; i--)
scanf("%lf%lf", &p[i].x, &p[i].y);
p[n] = p[0];
for (int i = 0; i < n; i++)
Sl[i] = (line){p[i + 1] - p[i], p[i], p[i + 1]};
if (Halfplane(Sl, n))
printf("YES\n");
else printf("NO\n");
}
return 0;
}
上一篇: MATLAB初探:美国人口与年份数据拟合
下一篇: JAVA学习——————Day18