UVA 10078 The Art Gallery

Problem:

Century Arts has hundreds of art galleries scattered all around the country and you are hired to write a program that determines whether any of the galleries has a critical point. The galleries are polygonal in shape and a critical point is a point inside that polygon from where the entire gallery is not visible.

Input:

The input file consists of several data blocks. Each data block describes one gallery.

The first line of a data block contains an integer N (3 <= N <= 50) indicating the number of corner points of the gallery. Each of the next N lines contains two integers giving the (xy) co-ordinates of a corner point where 0 <= xy <= 1000. Starting from the first point given in the input the corner points occur in the same order on the boundary of the gallery as they appear in the input. No three consecutive points are co-linear.

The input file terminates with a value of 0 for N.

Output:

For each gallery in the input output the word "Yes" if the gallery contains a critical point, otherwise output the word "No". Each output must be on a separate line.

解法:

1:首先可以知道:如果多边形是凸多边形,那么就没有满足题意的这样一个点。所以,我们可以先计算原多边形的面积area1,然后再求凸包,计算凸包的面积area2,如果原多边形是凸多边形,那么area1=area2,否则不相等,这道题就得到了解了。

2:计算原多边形的内核点集及其面积,然后判断这个面积和原多边形的面积是否相等。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 #define exp 1e-10
 9 #define PI 3.141592654
10 using namespace std;
11 const int maxn=55;
12 int n,m;
13 struct Point
14 {
15     double x,y;
16     Point(double x=0,double y=0):x(x),y(y){}
17     bool friend operator < (Point a,Point b)
18     {
19         if (a.x!=b.x) return a.x<b.x;
20         return a.y<b.y;
21     }
22 }an[maxn],bn[maxn];
23 typedef Point Vector;
24 Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }
25 Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
26 Vector operator * (Vector A,double p) {return Vector(A.x*p , A.y*p); }
27 Vector operator / (Vector A,double p) {return Vector(A.x/p , A.y/p); }
28 int dcmp(double x) {if (fabs(x)<exp) return 0;return x>0 ? 1 : -1 ;  }
29 double cross(Vector A,Vector B)
30 {
31     return A.x*B.y-B.x*A.y;
32 }
33 double get_area(Point *p,int n)
34 {
35     double area=0;
36     for (int i=2 ;i<n ;i++)
37         area += fabs(cross(p[i]-p[0],p[i-1]-p[0]));
38     return area;
39 }
40 int ConvexHull(Point *p,int n,Point *ch)
41 {
42     sort(p,p+n);
43     int m=0;
44     for (int i=0 ;i<n ;i++)
45     {
46         while (m>1 && dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-1]))<0) m--;
47         ch[m++]=p[i];
48     }
49     int k=m;
50     for (int i=n-2 ;i>=0 ;i--)
51     {
52         while (m>k && dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-1]))<0) m--;
53         ch[m++]=p[i];
54     }
55     if (n>1) m--;
56     return m;
57 }
58 int main()
59 {
60     while (scanf("%d",&n)!=EOF && n)
61     {
62         for (int i=0 ;i<n ;i++) scanf("%lf%lf",&an[i].x,&an[i].y);
63         double area1=get_area(an,n);
64         int k=ConvexHull(an,n,bn);
65 //        cout<<"DEBUG"<<endl;
66 //        for (int i=0 ;i<k ;i++) cout<<an[i].x<<" "<<an[i].y<<endl;
67 //        cout<<"n && k = "<<n<<" "<<k<<endl;
68 //
69         double area2=get_area(bn,k);
70         //cout<<area1<<"   **area**   "<<area2<<endl;
71         if (fabs(area1-area2)<exp) printf("No
");
72         else printf("Yes
");
73     }
74     return 0;
75 }
原文地址:https://www.cnblogs.com/huangxf/p/3928419.html