首页 > ACM题库 > HDU-杭电 > Hdu 1616 SCUD Busters-计算几何[解题报告] C++
2013
12-16

Hdu 1616 SCUD Busters-计算几何[解题报告] C++

SCUD Busters

问题描述 :

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 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.

输入:

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≤10) 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.

输出:

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

样例输入:

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

样例输出:

70.50
Hint

Hint

You may or may not find the following formula useful. Given a polygon described by the vertices v0,v1..vn such that v0=vn the signed area of the polygon is given by o_fom where the x, y coordinates of vi=(xi,yi) ; the edges of the polygon are from v(i) to v(i+1) for i=0..n-1 . If the points describing the polygon are given in a counterclockwise direction, the value of a will be positive, and if the points of the polygon are listed in a clockwise direction, the value of a will be negative.

计算几何的综合题。在一张500×500的地图上有若干个王国,每个王国都有若干个供电站,王国的边界为包含所有供电站的最小多边形。给出数次导弹攻击的座标,每次导弹攻击会使这个王国中所有的供电站失效。求最后所有没电的王国总面积。

分为三步做:1. 计算凸包,2. 计算多边形面积,3. 判断点是否在多边形内。

 

1. 计算凸包

什么是凸包?(摘自维基百科):

 

在一个实数向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X凸包

 S := \bigcap_{X \subseteq K \subseteq V \atop K\ \mathrm{is\ convex}} K.

X的凸包可以用X内所有点(x_1, \ldots, x_n)的线性组合来构造。

 S := \left\{ \left. \, \sum_{j=1}^n t_j x_j\, \right| x_j \in X,\, \sum_{j=1}^n t_j = 1,\, t_j \in \lbrack 0, 1 \rbrack \, \right\}.

在二维欧几里得空间中,凸包可想象为一条刚好包著所有点的橡皮圈。

简单的说,凸包即一个恰好包含所有点的,面积最小的多边形。

求凸包的方法之一:Graham扫描法。

从最下(若有多个取最左)的点A0开始,计算它与其他点的连线与X轴的角度,令dx=x1-x2, dy=y1-y2,则angle=acos(dx/sqrt(dx*dx+dy*dy))。

按angle的大小从小到大排序。设排序后的顶点序列为A1,A2,…,An.

令A0入队,从A1开始处理。设当前顶点为An

若队列仅有一个元素,则Ak入队;

若Ak-2,Ak-1,Ak所成的角向外(从向量Ak-1 – Ak-2的方向上观察An在向量左侧),则Ak入队;

若Ak-2,Ak-1,Ak所成的角向内,则弹出Ak-1,重新处理Ak。

最后队列中的所有顶点组成A0..An的凸包。

 

2. 计算多边形面积

设顶点为(x1,y1) (x2,y2) (x3,y3) … (xn, yn)则多边形面积

S = 0.5 * (x1 * y2 + x2 * y3 + x3 * y4 + … + xn-1 * yn + xn * y1 – x2 * y1 – x3 * y2 – x4 * y3 – … – xn * yn-1 – x1 * yn)

 

3. 判断点是否在多边形内

设按逆时针排序的顶点A0, A1, …, An,得向量序列A1-A0,A2-A1,A3-A2,…,An – An-1,

若点总在序列中向量的左侧,则点在多边形内;反之不在。

 

源代码:

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

using namespace std;

#define  pb push_back
const int maxn=100;
const double eps=1e-8;
const double pi=acos(-1.0);

inline double sqr(double x)
{
    return x*x;
}

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

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

struct point
{
    double x,y;
    point(){};
    point(double a,double b):x(a),y(b){}
    void input()
    {
        scanf("%lf%lf",&x,&y);
    }
    friend point operator + (const point &a,const point &b)
    {
        return point(a.x+b.x,a.y+b.y);
    }
    friend point operator - (const point &a,const point &b)
    {
        return point(a.x-b.x,a.y-b.y);
    }
    friend bool operator == (const point &a,const point &b)
    {
        return sig(a.x-b.x)==0 && sig(a.y-b.y)==0;
    }
    friend point operator * (const point &a,const double &b)
    {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (const double &a,const point &b)
    {
        return point(a*b.x,a*b.y);
    }
    friend point operator / (const point &a,const double &b)
    {
        return point(a.x/b,a.y/b);
    }
    double norm()
    {
        return sqrt(sqr(x)+sqr(y));
    }
};

double det(const point &a,const point &b)
{
    return a.x*b.y-a.y*b.x;
}

double dot(const point &a,const point &b)
{
    return a.x*b.x+a.y*b.y;
}

double dist(const point &a,const point &b)
{
    return (a-b).norm();
}

bool PointOnSegment(point p,point s,point t)
{
    return sig(det(p-s,t-s))==0 && sig(dot(p-s,p-t))<=0;
}

struct polygon
{
    int n;
    point a[maxn];
    polygon(){}
    int judDir()
    {
        int d;
        a[n]=a[0];
        for(int i=0;i<n-1;i++)
        {
            d=sig(det(a[i+1]-a[i],a[i+2]-a[i+1]));
            if(d!=0) break;
        }
        return d;
    }
    void to_counter_clockwise()
    {
        for(int i=0;i<(n)/2;i++)  swap(a[i],a[n-1-i]);
    }
    double area()
    {
        double sum=0;
        a[n]=a[0];
        for(int i=0;i<n;i++) sum+=det(a[i+1],a[i]);
        return sum/2;
    }
    int Point_In(point t)
    {
        int num=0,d1,d2,k;
        a[n]=a[0];
        for(int i=0;i<n;i++)
        {
            if(PointOnSegment(t,a[i],a[i+1])) return 2;
            k=sig(det(a[i+1]-a[i],t-a[i]));
            d1=sig(a[i].y-t.y);
            d2=sig(a[i+1].y-t.y);
            if(k>0 && d1<=0 && d2>0) num++;
            if(k<0 && d2<=0 && d1>0) num--;
        }
        return num!=0;
    }
};

struct polygon_convex
{
    vector<point> p;
    polygon_convex(int Size=0){ p.resize(Size);}
    polygon_convex operator = (polygon_convex x)
    {
        p.clear();
        for(int i=0;i<x.p.size();i++)
            p.push_back(x.p[i]);
        return *this;
    }
};

bool cmp_less(const point &a,const point &b)
{
    return sig(a.x-b.x)<0||sig(a.x-b.x)==0&&sig(a.y-b.y)<0;
}

polygon_convex convex_hull(vector<point> a)
{
    polygon_convex res(2*a.size()+5);
    sort(a.begin(),a.end(),cmp_less);
    a.erase(unique(a.begin(),a.end()),a.end());
    int m=0;
    for(int i=0;i<a.size();i++)
    {
        while(m>1 && sig(det(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=int(a.size())-2;i>=0;i--)
    {
        while(m>k&&sig(det(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(a.size()>1) res.p.resize(m-1);
    return res;
}

polygon plg[22];
polygon_convex c[22];
double a[22];
bool vis[22];
int n;

int main()
{
    int tp=0;
    point p;
    vector<point> pl;
    while(cin>>n && n!=-1)
    {
        pl.clear();
        for(int i=0;i<n;i++)
        {
            p.input();
            pl.pb(p);
        }
        c[tp]=convex_hull(pl);
        plg[tp].n=c[tp].p.size();
        for(int i=0;i<plg[tp].n;i++) plg[tp].a[i]=c[tp].p[i];
        if(plg[tp].judDir()==1) plg[tp].to_counter_clockwise();
        a[tp]=plg[tp].area();
        vis[tp++]=0;
    }
    double busted=0;
    double x,y;
    while(cin>>x>>y)
    {
        p.x=x;
        p.y=y;
        for(int i=0;i<tp;i++)
        {
            if(plg[i].Point_In(p)>0 && !vis[i])
            {
                vis[i]=1;
                busted+=a[i];
            }
        }
    }
    printf("%.2f\n",busted);
    return 0;
}

 


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. This write-up presents the gentle in which we can notice the fact. this is extremely wonderful one and gives in depth info.