Video Surveillance - POJ 1474(判断是否存在内核)

题目大意:询问是否在家里装一个监视器就可以监控所有的角落。

分析:赤裸裸的判断多边形内核题目。

 

代码如下:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;

const int MAXN = 107;
const int oo = 1e9+7;
const double EPS = 1e-10;

int Sign(double t)
{
    if(t > EPS)
        return 1;
    if(fabs(t) < EPS)
        return 0;
    return -1;
}

struct Point
{
    double  x, y;
    Point(double x=0, double y=0):x(x),y(y){}
    Point operator - (const Point &t)const{
        return Point(x-t.x, y-t.y);
    }
    double operator ^(const Point &t)const{
        return x*t.y - y*t.x;
    }

}p[MAXN], in[MAXN];
struct Segment
{
    Point S, E;
    double a, b, c;
    Segment(Point S=0, Point E=0):S(S), E(E){
        a = S.y - E.y;
        b = E.x - S.x;
        c = E.x*S.y - S.x*E.y;
    }
    Point crossNode(const Segment &t)const{
        Point res;

        res.x = (c*t.b-t.c*b) / (a*t.b-t.a*b);
        res.y = (c*t.a-t.c*a) / (b*t.a-t.b*a);

        return res;
    }
    int Mul(const Point &t)
    {///用叉积判断方向
        return Sign((E-S)^(t-S));
    }
};
int CutPoly(Segment L, int N)
{
    Point tmp[MAXN];
    int cnt = 0;

    for(int i=1; i<=N; i++)
    {
        if(L.Mul(in[i]) <= 0)
            tmp[++cnt] = in[i];
        else
        {
            if(L.Mul(in[i-1]) < 0)///求出交点
                tmp[++cnt] = L.crossNode(Segment(in[i-1],in[i]));
            if(L.Mul(in[i+1]) < 0)
                tmp[++cnt] = L.crossNode(Segment(in[i],in[i+1]));
        }
    }

    for(int i=1; i<=cnt; i++)
        in[i] = tmp[i];
    in[0] = in[cnt], in[cnt+1] = in[1];

    return cnt;
}

int main()
{
    int N, t=1;

    while(scanf("%d", &N) != EOF && N)
    {
        int M;
        double s=0;

        for(int i=1; i<=N; i++)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
            in[i] = p[i];
            if(i != 1)
                s += p[i].x*p[i-1].y - p[i].y*p[i-1].x;
        }
        s += p[1].x*p[N].y - p[1].y*p[N].x;
        if(s < 0)
        {
            for(int i=1; i<=N/2; i++)
            {
                swap(p[i], p[N-i+1]);
                swap(in[i], in[N-i+1]);
            }
        }

        in[0] = p[0] = p[N];
        in[N+1] = p[N+1] = p[1];
        M = N;

        for(int i=1; i<=N; i++)
            M = CutPoly(Segment(p[i],p[i+1]), M);

        printf("Floor #%d\n", t++);
        if(M > 0)
            printf("Surveillance is possible.\n\n");
        else
            printf("Surveillance is impossible.\n\n");
    }

    return 0;
}

 

posted @ 2015-10-29 15:35  无忧望月  阅读(253)  评论(0编辑  收藏  举报
levels of contents