Rotating Scoreboard
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 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 Sample Output YES NO Source |
[Submit] [Go Back] [Status] [Discuss]
问题描述:
判断多边形内是否存在一点使得这点都可以观察到边上的任一点(也可以是边上的任何一点都可以观察到这一点,也可以这样认为。实际上题目就是这个意思。。呵呵)
题目分析:
利用半平面交求多边形的内核!
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <fstream>
using namespace std;
const int N = 105;
struct point
{
double x, y;
}ps[N], des[N], kernel[N];
int pn;
double MaxDist; //点和点之间的最大距离,作为延伸线段用
//计算多边形的有向面积,按这种方法,顺时针为负
double getArea(point* ps, int pn)
{
double ans = 0;
int i;
ps[pn] = ps[0];
for (i = 0; i < pn; i++) ans += (ps[i].x * ps[i + 1].y - ps[i].y * ps[i + 1].x);
return ans / 2.0;
}
void revPs(point* ps, int pn) //把ps中的点反序
{
int l, r;
l = 0, r = pn - 1;
point tmp;
while (l < r)
{
tmp = ps[l];
ps[l] = ps
;
ps
= tmp;
l++, r--;
}
}
inline double dist(point& p1, point& p2)//求的是点p1和点p2的距离
{
return sqrt(pow(p1.x - p2.x, 2.0) + pow(p1.y - p2.y, 2.0));
}
void extLine(point& st, point& ed, double l)//延伸线段,l为延伸的长度
{
point tst = st, ted = ed;
double d = dist(tst, ted);
double dx, dy;
dx = (ted.x - tst.x) * l / d;
dy = (ted.y - tst.y) * l / d;
ed.x = dx + tst.x;
ed.y = dy + tst.y;
dx = -dx;
dy = -dy;
st.x = dx + ted.x;
st.y = dy + ted.y;
}
inline double xmul(point st1, point ed1, point st2, point ed2)//差积定理
{
return (ed1.x - st1.x) * (ed2.y - st2.y) - (ed1.y - st1.y) * (ed2.x - st2.x);
}
point interPoint(point st1, point ed1, point st2, point ed2) //得到内核
{
double eds = fabs(xmul(st1, ed1, st1, ed2));
double sts = fabs(xmul(st1, ed1, st1, st2));
point dd;
dd.x = (eds * st2.x + sts * ed2.x) / (eds + sts);
dd.y = (eds * st2.y + sts * ed2.y) / (eds + sts);
return dd;
}
//计算org在由st指向ed的线段的左侧的部分,一般来说st和ed要延伸
void semiPlane(point* org, int on, point* des, int& dn, point st, point ed)
{
dn = 0;
extLine(st, ed, MaxDist);
org[on] = org[0];
int i, j;
double ps, pe;
for (j = 0; j < on; j++)
{
ps = xmul(st, ed, st, org[j]);
pe = xmul(st, ed, st, org[j + 1]);
if (ps >= 0) des[dn++] = org[j];
if (ps * pe < 0)
{
des[dn++] = interPoint(st, ed, org[j], org[j + 1]);
}
}
}
//获得org的内核,放在kernel里
void getKernel(point* org, int on, point* kernel, int& kn)
{
if (getArea(org, on) < 0)
{
revPs(org, on);
}
org[on] = org[0];
int i, dn, j;
MaxDist = 0;
for (i = 0; i < on; i++)
{
for (j = i + 1; j < on; j++) MaxDist = max(MaxDist, dist(org[i], org[j]));
}
for (i = 0; i < on; i++) kernel[i] = org[i];
kn = on;
for (i = 0; i < on; i++)
{
semiPlane(kernel, kn, des, dn, org[i], org[i + 1]);
for (j = 0; j < dn; j++) kernel[j] = des[j];
kn = dn;
}
}
int main()
{
int i, t, kn;
scanf("%d", &t);
while (t--)
{
scanf("%d", &pn);
for (i = 0; i < pn; i++) scanf("%lf%lf", &ps[i].x, &ps[i].y);
getKernel(ps, pn, kernel, kn);
if (!kn) printf("NO\n");
else printf("YES\n");
}
return 0;
}