POJ 1264 SCUD Busters 求凸包面积+判断一个点是否在凸包内

http://poj.org/problem?id=1264
Description
Some problems are difficult to solve but have a simplification that is easy to solve. Rather than deal with the difficulties of constructing a model of the Earth (a somewhat oblate spheroid), consider a pre-Columbian flat world that is a 500 kilometer x 500 kilometer square.
In the model used in this problem, the flat world consists of several warring kingdoms. Though warlike, the people of the world are strict isolationists; each kingdom is surrounded by a high (but thin) wall designed to both protect the kingdom and to isolate it. To avoid fights for power, each kingdom has its own electric power plant.
When the urge to fight becomes too great, the people of a kingdom often launch missiles at other kingdoms. Each SCUD missile (anitary leansing niversal estroyer) that lands within the walls of a kingdom destroys that kingdom’s power plant (without loss of life).

Given coordinate locations of several kingdoms (by specifying the locations of houses and the location of the power plant in a kingdom) and missile landings you are to write a program that determines the total area of all kingdoms that are without power after an exchange of missile fire.
In the simple world of this problem kingdoms do not overlap. Furthermore, the walls surrounding each kingdom are considered to be of zero thickness. The wall surrounding a kingdom is the minimal-perimeter wall that completely surrounds all the houses and the power station that comprise a kingdom; the area of a kingdom is the area enclosed by the minimal-perimeter thin wall.
There is exactly one power station per kingdom.
There may be empty space between kingdoms.

Input
The input is a sequence of kingdom specifications followed by a sequence of missile landing locations.
A kingdom is specified by a number N (3 <= N <= 100) on a single line which indicates the number of sites in this kingdom. The next line contains the x and y coordinates of the power station, followed by N-1 lines of x, y pairs indicating the locations of homes served by this power station. A value of -1 for N indicates that there are no more kingdoms. There will be at least one kingdom in the data set.
Following the last kingdom specification will be the coordinates of one or more missile attacks, indicating the location of a missile landing. Each missile location is on a line by itself. You are to process missile attacks until you reach the end of the file.
Locations are specified in kilometers using coordinates on a 500 km by 500 km grid. All coordinates will be integers between 0 and 500 inclusive. Coordinates are specified as a pair of integers separated by white-space on a single line. The input file will consist of up to 20 kingdoms, followed by any number of missile attacks.

Output
The output consists of a single number representing the total area of all kingdoms without electricity after all missile attacks have been processed. The number should be printed with (and correct to) two decimal places.

Sample Input

12
3 3
4 6
4 11
4 8
10 6
5 7
6 6
6 3
7 9
10 4
10 9
1 7
5
20 20
20 40
40 20
40 40
30 30
3
10 10
21 10
21 13
-1
5 5
20 12

Sample Output

70.50

Source
Duke Internet Programming Contest 1991,UVA 109
题目大意:这题估计就是题目太长才没人做。给出数个王国的城镇分布,每个王国有 n n n个城镇,且王国的面积就是覆盖这 n n n个城镇的凸包,保证王国与王国之间不相交,当 n = − 1 n=-1 n=1时开始给出导弹袭击的坐标 ( x , y ) (x,y) (x,y),若这个导弹降落到了王国 i i i,那么这个王国的电力就瘫痪了,最后让你输出电力瘫痪的总面积。
思路:初始计算凸包的部分就不说了。对于每一个导弹 ( x , y ) (x,y) (x,y),我们对每一个电力还未瘫痪的王国进行计算,看这个点是不是在凸包内部,如果是就给这个王国打上标记。最后计算被打上标记的王国的总面积就行了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

const double pi=acos(-1);//弧度pi
const double eps=1e-8;//精度

inline int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x>0)
        return 1;
    return -1;
}

struct point
{
    double x,y;
    point(double a=0,double b=0)
    {
        x=a,y=b;
    }
    friend point operator * (point a,double b)
    {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (double a,point b)
    {
        return point(b.x*a,b.y*a);
    }
    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);
    }
    point operator / (const double b)const
    {
        return point(x/b,y/b);
    }
    bool operator < (const point &b)const//按坐标排序
    {
        if(fabs(x-b.x)<eps)
            return y<b.y-eps;
        return x<b.x-eps;
    }
    bool operator == (const point &b)const
    {
        return sgn(x-b.x)==0&&sgn(y-b.y)==0;
    }
    void transxy(double sinb,double cosb)//逆时针旋转b弧度
    {                                      //若顺时针 在传入的sinb前加个-即可
        double tx=x,ty=y;
        x=tx*cosb-ty*sinb;
        y=tx*sinb+ty*cosb;
    }
    void transxy(double b)//逆时针旋转b弧度
    {                     //若顺时针传入-b即可
        double tx=x,ty=y;
        x=tx*cos(b)-ty*sin(b);
        y=tx*sin(b)+ty*cos(b);
    }
    double norm()
    {
        return sqrt(x*x+y*y);
    }
};

inline double dot(point a,point b)//点积
{
    return a.x*b.x+a.y*b.y;
}
inline double cross(point a,point b)//叉积
{
    return a.x*b.y-a.y*b.x;
}

inline double dist(point a,point b)//两点间距离
{
    return (a-b).norm();
}

typedef point Vector;

vector<point> a;

struct polygon_convex//凸包
{
    vector<point> p;
    polygon_convex(int siz=0)
    {
        p.resize(siz);
    }
    double perimeter()//计算多边形周长
    {
        double sum=0;
        int len=p.size();
        for(int i=0;i<len-1;i++)
            sum+=(p[i+1]-p[i]).norm();
        sum+=(p[0]-p[len-1]).norm();
        return sum;
    }
    double area()//计算多边形有向面积
    {
        double sum=0;
        int len=p.size();
        for(int i=0;i<len-1;i++)
            sum+=cross(p[i+1],p[i]);
        sum+=cross(p[0],p[len-1]);
        return sum/2;
    }
    double final_area()//多边形面积 即一定为正数
    {
        return fabs(area());
    }
    int contain(const point b)//lgn的复杂度下判断点b是否在凸包内 true表示在凸包内(或边界上)
    {
        int n=p.size();
        point g=(p[0]+p[n/3]+p[2*n/3])/3.0;
        int l=0,r=n,mid;//二分凸包
        while(l+1<r)
        {
            mid=(l+r)>>1;
            if(sgn(cross(p[l]-g,p[mid]-g))>0)
            {
                if(sgn(cross(p[l]-g,b-g))>=0&&sgn(cross(p[mid]-g,b-g))<0)
                    r=mid;
                else
                    l=mid;
            }
            else
            {
                if(sgn(cross(p[l]-g,b-g))<0&&sgn(cross(p[mid]-g,b-g))>=0)
                    l=mid;
                else
                    r=mid;
            }
        }
        r%=n;
        int z=sgn(cross(p[r]-b,p[l]-b));
        return z;//-1 内部 0 边界 1 外部
    }
};

polygon_convex convex_hull()//用a中的点求解出凸包
{
    polygon_convex res(2*a.size()+5);
    sort(a.begin(),a.end());//按照横坐标排序
    a.erase(unique(a.begin(),a.end()),a.end());//去重
    int m=0;
    int len=a.size();
    for(int i=0;i<len;i++)//求下凸包
    {
        while(m>1&&sgn(cross(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0)//不包括共线点 如果包括共线点请修改此处的<=为<
            --m;
        res.p[m++]=a[i];
    }
    int k=m;
    for(int i=len-2;i>=0;i--)//求上凸包
    {
        while(m>k&&sgn(cross(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0)//不包括共线点 如果包括共线点请修改此处的<=为<
            --m;
        res.p[m++]=a[i];
    }
    res.p.resize(m);
    if(len>1)//去重
        res.p.resize(m-1);
    return res;
}

polygon_convex pc[105];
int cnt=0;
int vis[105];

int main()
{
    int n;
    point tmp;
    while(~scanf("%d",&n)&&n!=-1)
    {
        a.resize(n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&tmp.x,&tmp.y);
            a[i]=tmp;
        }
        pc[cnt++]=convex_hull();
    }
    double sum=0;
    while(~scanf("%lf%lf",&tmp.x,&tmp.y))
    {
        for(int i=0;i<cnt;i++)
            if(!vis[i]&&pc[i].contain(tmp)==-1)
                vis[i]=1;
    }
    for(int i=0;i<cnt;i++)
        if(vis[i])
            sum+=pc[i].final_area();
    printf("%.2f\n",sum);
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值