HDU 1469(Video Surveillance)

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 1005;

struct Point //点
{
    double x, y; //坐标
    Point() {}
    Point(double x, double y) : x(x), y(y) {}
    Point operator+(const Point& b) const { return Point(x + b.x, y + b.y); }
    Point operator-(const Point& b) const { return Point(x - b.x, y - b.y); }
    double operator*(const Point& b) const { return x * b.y - y * b.x; }
    Point operator*(double b) const { return Point(b * x, b * y); }
}p[MAXN];

struct Line //线
{
    Point st, ed; //起点,终点
    double angle; //与X轴的夹角(弧度)
    Line() {}
    Line(const Point& st, const Point& ed) : st(st), ed(ed), angle(atan2(ed.y, ed.x)) { }

    bool operator <(const Line& b) const
    {
        if (angle == b.angle)
            return ed * (b.st - st) < 0;
        return
            angle < b.angle;
    }
    bool operator ==(const Line& b) const { return angle == b.angle; }

    Point intersect(const Line& b)
    {
        Point u = st - b.st;
        double t = (b.ed * u) / (ed * b.ed);
        return st + ed * t;
    }
}line[MAXN];

bool right(const Point& p, const Line& l)
{
    return l.ed * (p - l.st) < 0;
}

int halfIntersection(Line l[], int n, Point* p)
{
    static Line q[MAXN];
    sort(l + 1, l + n + 1);
    n = unique(l + 1, l + n + 1) - (l + 1);
    int f = 1, r = 0, ans = 0;
    q[++r] = l[1]; q[++r] = l[2];
    for (int i = 3; i <= n; i++)
    {
        while (f < r && right(q[r].intersect(q[r - 1]), l[i]))
            r--;
        while (f < r && right(q[f].intersect(q[f + 1]), l[i]))
            f++;
        q[++r] = l[i];
    }
    while (f < r && right(q[r].intersect(q[r - 1]), q[f]))
        r--;
    while (f < r && right(q[f].intersect(q[f + 1]), q[r]))
        f++;
    q[r + 1] = q[f];
    for (int i = f; i <= r; i++)
        p[++ans] = q[i].intersect(q[i + 1]);
    return ans;
}

int main() 
{
    int Case = 1;
    int n;
    while (cin >> n)
    {
        if (n == 0)
            break;

        for (int i = 1; i <= n; i++)
        {
            cin >> p[i].x >> p[i].y;
        }
            
        p[n + 1] = p[1];
        int numLine = 0;
        for (int i = 1; i <= n; i++)
        {
            line[++numLine] = Line(p[i], p[i] - p[i + 1]);
        }
            
        int ans = halfIntersection(line, numLine, p);
        cout << "Floor #" << Case++ << endl;
        if (ans > 2)
            cout << "Surveillance is possible." << endl;
        else
            cout << "Surveillance is impossible." << endl;
        cout << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值