SCUD Busters POJ - 1264(凸包)

题意:有n个国家,输入国家的坐标让你构建国家的凸包,然后输入炮弹,当炮弹在这个国家里的话国家就被炸毁了求最后被炸毁的总面积。

wa的点!!!!有可能反复炸一个国家不能重复计算面积

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;

const int maxn=200;
const double eps=1e-8;


int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
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);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};

struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s=_s;
        e=_e;
    }
};
int n;
Point p[maxn];
int Stack[maxn];
int top=0;

double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
bool cmp(Point a,Point b)
{
    int ans=sgn((a-p[0])^(b-p[0]));
    if(ans==1)
        return true;
    else if(ans==0)
        return dist(a,p[0])<dist(b,p[0]);
    else
        return false;
}
void graham()
{
    for(int i=0;i<n;i++)
    {
        if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
            swap(p[i],p[0]);
    }
    sort(p+1,p+n,cmp);
    if(n==1)
    {
        Stack[0]=0;
        top=1;
    }
    else if(n==2)
    {
        Stack[0]=0;
        Stack[1]=1;
        top=2;
    }
    else
    {
        Stack[0]=0;
        Stack[1]=1;
        top=2;
        for(int i=2;i<n;i++)
        {
            while(top>1&&((p[Stack[top-1]]-p[Stack[top-2]])^(p[i]-p[Stack[top-2]]))<=0)
                top--;
            Stack[top++]=i;
        }
    }
}

vector <Point> ve[maxn];

double CalcArea(vector <Point> &ve)
{
    double res=0;
    for(int i=0;i<ve.size();i++)
    {
        res+=(ve[i]^ve[(i+1)%ve.size()])/2;
    }
    return fabs(res);
}

bool OnSeg(Point p,Line l)
{
    return sgn((l.s-p)^(l.e-p))==0&&
    sgn((p.x-l.s.x)*(p.x-l.e.x))<=0&&
    sgn((p.y-l.s.y)*(p.y-l.e.y))<=0;
}

int inConvexPoly(Point p1,vector <Point> &ve)
{
    for(int i=0;i<ve.size();i++)
    {
        if(sgn((ve[i]-p1)^(ve[(i+1)%(ve.size())]-p1))<0)
            return -1;
        else if(OnSeg(p1,Line(ve[i],ve[(i+1)%(ve.size())])))
            return 0;
    }
    return 1;
}
int main()
{
    int cont=0;
    double ans=0;
    while(1)
    {
        scanf("%d",&n);
        if(n==-1)
            break;
        for(int i=0;i<n;i++)
            scanf("%lf %lf",&p[i].x,&p[i].y);
        top=0;
        graham();
        for(int i=0;i<top;i++)
        {
            ve[cont].push_back(p[Stack[i]]);
        }
        //ans+=CalcArea(ve[cont]);
        cont++;
    }
    //printf("%.2lf\n",ans);
    double x,y;
    int check[maxn]={0};
    while(scanf("%lf %lf",&x,&y)!=EOF)
    {
        Point p1=Point(x,y);
        for(int i=0;i<cont;i++)
        {
            if(inConvexPoly(p1,ve[i])>=0&&check[i]==0)
            {
                check[i]=1;
                ans+=CalcArea(ve[i]);
                continue;
            }
        }
    }
    printf("%.2lf\n",ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值