首页 > 代码库 > 半平面交模板

半平面交模板

POJ 1474
Video Surveillance
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3392 Accepted: 1497

Description

A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- ment store. One of his tasks is to install a video surveillance system to guarantee the security of the customers (and the security of the merchandise of course) on all of the store‘s countless floors. As the company has only a limited budget, there will be only one camera on every floor. But these cameras may turn around to look in every direction.
The first problem is to choose where to install the camera for every floor. The only requirement is that every part of the room must be visible from there. In the following figure the left floor can be completely surveyed from the position indicated by a dot, while for the right floor, there is no such position, the given position failing to see the lower left part of the floor.
Before trying to install the cameras, your friend first wants to know whether there is indeed a suitable position for them. He therefore asks you to write a program that, given a ground plan, de- termines whether there is a position from which the whole floor is visible. All floor ground plans form rectangular polygons, whose edges do not intersect each other and touch each other only at the corners.

Input

The input contains several floor descriptions. Every description starts with the number n of vertices that bound the floor (4 <= n <= 100). The next n lines contain two integers each, the x and y coordinates for the n vertices, given in clockwise order. All vertices will be distinct and at corners of the polygon. Thus the edges alternate between horizontal and vertical.
A zero value for n indicates the end of the input.

Output

For every test case first output a line with the number of the floor, as shown in the sample output. Then print a line stating "Surveillance is possible." if there exists a position from which the entire floor can be observed, or print "Surveillance is impossible." if there is no such position.
Print a blank line after each test case.

Sample Input

40 00 11 11 080 00 21 21 12 12 23 23 00

Sample Output

Floor #1Surveillance is possible.Floor #2Surveillance is impossible.
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define EPS 1e-8#define N 1010int n;int dq[N];int top,bot,pn;int dump(double x){    if(fabs(x)<EPS) return 0;    return x>0?1:-1;}struct Point{    double x,y;    Point (){}    Point (double x,double y):x(x),y(y){}    Point operator - (Point p){        return Point(x-p.x,y-p.y);    }    Point operator + (Point p){        return Point(x+p.x,y+p.y);    }    Point operator * (double d){        return Point(x*d,y*d);    }    double operator ^ (Point p){        return x*p.y-y*p.x;    }};struct Line{    Point s,e;    double k;    Line(){}    Line(Point s,Point e):s(s),e(e){        k=atan2(e.y-s.y,e.x-s.x);    }    Point operator & (Line l){        return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s)));    }};Line l[N];Point p[N],q[N];bool HPIcmp(Line a,Line b){    int d=dump(a.k-b.k);    if(!d) return dump((b.s-a.s)^(b.e-a.s))>0;    return d<0;}bool Judge(Line a,Line b,Line c){    Point p=b&c;    return dump((a.s-p)^(a.e-p))<0;}void HPI(){    int i,j;    sort(l,l+n,HPIcmp);    for(i=0,j=0;i<n;i++)    {        if(dump(l[i].k-l[j].k)>0) l[++j]=l[i];    }    n=j+1;    dq[0]=0;    dq[1]=1;    top=1;    bot=0;    for(i=2;i<n;i++)    {        while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-1]])) top--;        while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++;        dq[++top]=i;    }    while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--;    while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++;    if(top-bot>=2)  cout<<"Surveillance is possible.\n\n";    else cout<<"Surveillance is impossible.\n\n";}int main(){    int T,iCase=1;    while(scanf("%d",&n),n)    {        for(int i=0;i<n;i++)        {            scanf("%lf%lf",&q[i].x,&q[i].y);            }        for(int i=0;i<n;i++)        {            l[i]=Line(q[i],q[(i-1+n)%n]);        }        printf("Floor #%d\n",iCase++);        HPI();    }    return 0;}

 

POJ 1279

Art Gallery
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5600 Accepted: 2372

Description

The art galleries of the new and very futuristic building of the Center for Balkan Cooperation have the form of polygons (not necessarily convex). When a big exhibition is organized, watching over all of the pictures is a big security concern. Your task is that for a given gallery to write a program which finds the surface of the area of the floor, from which each point on the walls of the gallery is visible. On the figure 1. a map of a gallery is given in some co-ordinate system. The area wanted is shaded on the figure 2.

Input

The number of tasks T that your program have to solve will be on the first row of the input file. Input data for each task start with an integer N, 5 <= N <= 1500. Each of the next N rows of the input will contain the co-ordinates of a vertex of the polygon ? two integers that fit in 16-bit integer type, separated by a single space. Following the row with the co-ordinates of the last vertex for the task comes the line with the number of vertices for the next test and so on.

Output

For each test you must write on one line the required surface - a number with exactly two digits after the decimal point (the number should be rounded to the second digit after the decimal point).

Sample Input

170 04 44 79 713 -18 -64 -4

Sample Output

80.00
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define EPS 1e-8#define N 1010int n;int dq[N];int top,bot,pn;int dump(double x){    if(fabs(x)<EPS) return 0;    return x>0?1:-1;}struct Point{    double x,y;    Point (){}    Point (double x,double y):x(x),y(y){}    Point operator - (Point p){        return Point(x-p.x,y-p.y);    }    Point operator + (Point p){        return Point(x+p.x,y+p.y);    }    Point operator * (double d){        return Point(x*d,y*d);    }    double operator ^ (Point p){        return x*p.y-y*p.x;    }};struct Line{    Point s,e;    double k;    Line(){}    Line(Point s,Point e):s(s),e(e){        k=atan2(e.y-s.y,e.x-s.x);    }    Point operator & (Line l){        return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s)));    }};Line l[N];Point p[N],q[N];bool HPIcmp(Line a,Line b){    int d=dump(a.k-b.k);    if(!d) return dump((b.s-a.s)^(b.e-a.s))>0;    return d<0;}bool Judge(Line a,Line b,Line c){    Point p=b&c;    return dump((a.s-p)^(a.e-p))<0;}void HPI(){    int i,j;    sort(l,l+n,HPIcmp);    for(i=0,j=0;i<n;i++)    {        if(dump(l[i].k-l[j].k)>0) l[++j]=l[i];    }    n=j+1;    dq[0]=0;    dq[1]=1;    top=1;    bot=0;    for(i=2;i<n;i++)    {        while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-1]])) top--;        while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++;        dq[++top]=i;    }    while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--;    while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++;    dq[++top]=dq[bot];    for(pn=0,i=bot;i<top;i++,pn++)    {        p[pn]=l[dq[i+1]]&l[dq[i]];    }}double GetArea(){    double marea=0;    if(pn<3) return 0;    for(int i=2;i<pn;i++)    {        marea+=(p[i]-p[0])^(p[i-1]-p[0]);    }    return fabs(marea)/2;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%lf%lf",&q[i].x,&q[i].y);            }        for(int i=0;i<n;i++)        {            l[i]=Line(q[i],q[(i-1+n)%n]);        }        HPI();        printf("%.2f\n",GetArea());    }    return 0;}

 

POJ 3130

How I Mathematician Wonder What You Are!
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3219 Accepted: 1723

Description

After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big astronomical telescope and lets his image processing program count stars. The hardest part of the program is to judge if shining object in the sky is really a star. As a mathematician, the only way he knows is to apply a mathematical definition of stars.

The mathematical definition of a star shape is as follows: A planar shape F is star-shaped if and only if there is a point C ∈ F such that, for any point P ∈ F, the line segment CP is contained in F. Such a point C is called a center of F. To get accustomed to the definition let’s see some examples below.

The first two are what you would normally call stars. According to the above definition, however, all shapes in the first row are star-shaped. The two in the second row are not. For each star shape, a center is indicated with a dot. Note that a star shape in general has infinitely many centers. Fore Example, for the third quadrangular shape, all points in it are centers.

Your job is to write a program that tells whether a given polygonal shape is star-shaped or not.

Input

The input is a sequence of datasets followed by a line containing a single zero. Each dataset specifies a polygon, and is formatted as follows.

n 
x1y1
x2y2

xnyn

The first line is the number of vertices, n, which satisfies 4 ≤ n ≤ 50. Subsequent n lines are the x- and y-coordinates of the n vertices. They are integers and satisfy 0 ≤ xi ≤ 10000 and 0 ≤ yi ≤ 10000 (i = 1, …, n). Line segments (xi, yi)–(xi + 1, yi + 1) (i = 1, …, n − 1) and the line segment (xn, yn)–(x1, y1) form the border of the polygon in the counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions.

You may assume that the polygon is simple, that is, its border never crosses or touches itself. You may assume assume that no three edges of the polygon meet at a single point even when they are infinitely extended.

Output

For each dataset, output “1” if the polygon is star-shaped and “0” otherwise. Each number must be in a separate line and the line should not contain any other characters.

Sample Input

6 66 13 96 61 76 98 13 94 4 0 45 68 8 27 21 55 14 93 12 56 95 15 48 38 46 51 65 64 31 0

Sample Output

10

 

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define EPS 1e-8#define N 1010int n;int dq[N];int top,bot,pn;int dump(double x){    if(fabs(x)<EPS) return 0;    return x>0?1:-1;}struct Point{    double x,y;    Point (){}    Point (double x,double y):x(x),y(y){}    Point operator - (Point p){        return Point(x-p.x,y-p.y);    }    Point operator + (Point p){        return Point(x+p.x,y+p.y);    }    Point operator * (double d){        return Point(x*d,y*d);    }    double operator ^ (Point p){        return x*p.y-y*p.x;    }};struct Line{    Point s,e;    double k;    Line(){}    Line(Point s,Point e):s(s),e(e){        k=atan2(e.y-s.y,e.x-s.x);    }    Point operator & (Line l){        return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s)));    }};Line l[N];Point p[N],q[N];bool HPIcmp(Line a,Line b){    int d=dump(a.k-b.k);    if(!d) return dump((b.s-a.s)^(b.e-a.s))>0;    return d<0;}bool Judge(Line a,Line b,Line c){    Point p=b&c;    return dump((a.s-p)^(a.e-p))>0;}void HPI(){    int i,j;    sort(l,l+n,HPIcmp);    for(i=0,j=0;i<n;i++)    {        if(dump(l[i].k-l[j].k)>0) l[++j]=l[i];    }    n=j+1;    dq[0]=0;    dq[1]=1;    top=1;    bot=0;    for(i=2;i<n;i++)    {        while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-1]])) top--;        while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++;        dq[++top]=i;    }    while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--;    while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++;    if(top-bot>=2)  cout<<"1\n";    else cout<<"0\n";}int main(){    int T;    while(scanf("%d",&n),n)    {        for(int i=0;i<n;i++)        {            scanf("%lf%lf",&q[i].x,&q[i].y);            }        for(int i=0;i<n;i++)        {            l[i]=Line(q[i],q[(i-1+n)%n]);        }        HPI();    }    return 0;}

 

POJ 3335

Rotating Scoreboard
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5439 Accepted: 2166

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

24 0 0 0 1 1 1 1 08 0 0  0 2  1 2  1 1  2 1  2 2  3 2  3 0

Sample Output

YESNO
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define EPS 1e-8#define N 1010int n;int dq[N];int top,bot,pn;int dump(double x){    if(fabs(x)<EPS) return 0;    return x>0?1:-1;}struct Point{    double x,y;    Point (){}    Point (double x,double y):x(x),y(y){}    Point operator - (Point p){        return Point(x-p.x,y-p.y);    }    Point operator + (Point p){        return Point(x+p.x,y+p.y);    }    Point operator * (double d){        return Point(x*d,y*d);    }    double operator ^ (Point p){        return x*p.y-y*p.x;    }};struct Line{    Point s,e;    double k;    Line(){}    Line(Point s,Point e):s(s),e(e){        k=atan2(e.y-s.y,e.x-s.x);    }    Point operator & (Line l){        return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s)));    }};Line l[N];Point p[N],q[N];bool HPIcmp(Line a,Line b){    int d=dump(a.k-b.k);    if(!d) return dump((b.s-a.s)^(b.e-a.s))>0;    return d<0;}bool Judge(Line a,Line b,Line c){    Point p=b&c;    return dump((a.s-p)^(a.e-p))<0;}void HPI(){    int i,j;    sort(l,l+n,HPIcmp);    for(i=0,j=0;i<n;i++)    {        if(dump(l[i].k-l[j].k)>0) l[++j]=l[i];    }    n=j+1;    dq[0]=0;    dq[1]=1;    top=1;    bot=0;    for(i=2;i<n;i++)    {        while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-1]])) top--;        while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++;        dq[++top]=i;    }    while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--;    while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++;    top-bot>=2?cout<<"YES\n":cout<<"NO\n";}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%lf%lf",&q[i].x,&q[i].y);            }        for(int i=0;i<n;i++)        {            l[i]=Line(q[i],q[(i-1+n)%n]);        }        HPI();    }    return 0;}

 

 

半平面交模板