HDU1616计算几何-凸包+点在多边形内+多边形的面积

来源:互联网 发布:淘宝黑搜索卡首页2017 编辑:程序博客网 时间:2024/06/16 19:55

SCUD Busters

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31    Accepted Submission(s): 14


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

 

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
123 34 64 114 810 65 76 66 37 910 410 91 7520 2020 4040 2040 4030 30310 1021 1021 13-15 520 12
 

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

Source
UVA
 
  1. 本题是比较简单的计算几何题,使用模板代码可以很轻松的 AC,解题思路是很直接的,和 UVa 12016 类  
  2. // 似。求出每个王国的凸包顶点,判断导弹的落点是否在某个王国的凸包范围内,若在其范围内,则该王国电力  
  3. // 丧失,该王国的面积计入丧失电力的总面积之中。

#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <list>#include <bitset>using namespace std ;#define zero {0}#define INF 0x3f3f3f3f#define EPS 1e-6typedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}#define N 100005template<class T> T sqr(T x)//求平方{    return x * x;}// Point classstruct Point;typedef Point Vec;struct Point{    double x, y;    Point () {}    Point(double a, double b)    {        x = a;        y = b;    }};Point p[20];Vec operator + (const Vec &a, const Vec &b) //点加法{    return Vec(a.x + b.x, a.y + b.y);}Vec operator - (const Vec &a, const Vec &b) //点减法{    return Vec(a.x - b.x, a.y - b.y);}Vec operator * (const Vec &a, const double &p) //点与常数相乘{    return Vec(a.x * p, a.y * p);}Vec operator / (const Vec &a, const double &p) //点除以常数{    return Vec(a.x / p, a.y / p);}bool operator < (const Vec &a, const Point &b) //平面直角坐标系中左下方的为小{    return a.x < b.x || (sgn(a.x - b.x) == 0 && a.y < b.y);}bool operator == (const Vec &a, const Point &b) //点相等判断{    return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}inline double ptDis(Point a, Point b)//点间距{    return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));}inline double dotDet(Vec a, Vec b)//点乘{    return a.x * b.x + a.y * b.y;}inline double crossDet(Vec a, Vec b)//叉乘{    return a.x * b.y - a.y * b.x;}inline double crossDet(Point o, Point a, Point b)//向量叉乘{    return crossDet(a - o, b - o);}inline double vecLen(Vec a)//求一点到原点距离{    return sqrt(dotDet(a, a));}// Polygon classstruct Poly//平面类{    vector<Point> pt;//保存平面对应的端点    bool flag;    Poly()    {        pt.clear();    }    ~Poly() {}    Poly(vector<Point> pt): pt(pt)    {        flag = 1;   //使用vector进行初始化    }    Point operator [](int x) const//重载[]返回对应端点    {        return pt[x];    }    int size()//返回对应平面点数    {        return pt.size();    }    void put()    {        for (int i = 0; i < pt.size(); i++)        {            printf("%lf %lf\n", pt[i].x, pt[i].y);        }        printf("\n");    }    double area()//得到面积,凹凸多边形均可    {        double ret = 0.0;        int sz = pt.size();        for (int i = 0; i < sz; i++)        {            int j = (i + 1) % sz;            ret += crossDet(pt[i], pt[j]);        }        return fabs(ret / 2.0);    }} ;inline bool onSeg(Point x, Point a, Point b)//判断点在线段ab上,加上||x==a||x==b在端点也算{    return (sgn(crossDet(a - x, b - x)) == 0 && sgn(dotDet(a - x, b - x)) < 0) || x == a || x == b;}int ptInPoly(Point p, Poly &poly)//判断点在多边形内,用vector<Point>初始化多边形,-1在边上,0在外面,1在里面{    int wn = 0, sz = poly.size();    for (int i = 0; i < sz; i++)    {        if (onSeg(p, poly[i], poly[(i + 1) % sz])) return -1;        int k = sgn(crossDet(poly[(i + 1) % sz] - poly[i], p - poly[i]));        int d1 = sgn(poly[i].y - p.y);        int d2 = sgn(poly[(i + 1) % sz].y - p.y);        if (k > 0 && d1 <= 0 && d2 > 0) wn++;        if (k < 0 && d2 <= 0 && d1 > 0) wn--;    }    if (wn != 0) return 1;    return 0;}///逆时针顺序把极角排序 以p[0](左下角点)为旋转变量bool cmp3( Point a, Point b){    int ret = crossDet(a - p[0], b - p[0]);    if (ret > 0)        return true;    if (ret == 0 && ptDis(p[0], a) < ptDis(p[0], b)) //保留凸包上的点,共线取最近        return true;    return false;}void PtAngleSort(Point *sp, Point *ep){    int n = ep - sp;    int k = 0;    for (int i = 1; i < n; i++)    {        if (sp[i].x < sp[k].x || (sp[i].x == sp[k].x && sp[i].y < sp[k].y))            k = i;    }    swap(sp[k], sp[0]);    sort(sp, ep, cmp3);}int andrew(Point *pt, int n, Point *ch){    sort(pt, pt + n);    int m = 0;    for (int i = 0; i < n; i++)    {        while (m > 1 && crossDet(ch[m - 1] - ch[m - 2], pt[i] - ch[m - 2]) <= 0) m--; //这里        ch[m++] = pt[i];    }    int k = m;    for (int i = n - 2; i >= 0; i--)    {        while (m > k && crossDet(ch[m - 1] - ch[m - 2], pt[i] - ch[m - 2]) <= 0) m--; //这里        ch[m++] = pt[i];    }    if (n > 1) m--;    return m;}int main(){#ifdef DeBUGs    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif    double sum = 0;    int n;    int cnt = 0;    Poly duo[100];    Point ch[100];    std::vector<Point> v;    while (scanf("%d", &n), n != -1)    {        v.clear();        for (int i = 0; i < n; i++)        {            scanf("%lf%lf", &p[i].x, &p[i].y);        }        int l = andrew(p, n, ch);        for (int i = 0; i < l; i++)        {            // printf("%lf %lf\n",ch[i].x,ch[i].y);            v.push_back(ch[i]);        }        duo[cnt++] = Poly(v);        // printf("\n");    }    // for (int i = 0; i < cnt; i++)    // {    //     duo[i].put();    // }    Point attack;    while (scanf("%lf%lf", &attack.x, &attack.y) + 1)    {        for (int i = 0; i < cnt; i++)        {            if (duo[i].flag && ptInPoly(attack, duo[i]) == 1)            {                duo[i].flag = 0;                sum += duo[i].area();                // cout << i << " " << sum << endl;                break;            }        }    }    printf("%.2lf\n", sum);    return 0;}


0 0
原创粉丝点击