• UVa 109


    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=45

     

     SCUD Busters 

    Background

    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 tex2html_wrap_inline41 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 (Sanitary Cleansing Universal Destroyer) that lands within the walls of a kingdom destroys that kingdom's power plant (without loss of life).

    The Problem

    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

    The input is a sequence of kingdom specifications followed by a sequence of missile landing locations.

    A kingdom is specified by a number N ( tex2html_wrap_inline45 ) 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 xy 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

    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

    A Hint

    You may or may not find the following formula useful.

    Given a polygon described by the vertices tex2html_wrap_inline61 such that tex2html_wrap_inline63 , the signed area of the polygon is given by

    displaymath59

    where the x, y coordinates of tex2html_wrap_inline65 ; the edges of the polygon are from tex2html_wrap_inline67 to tex2html_wrap_inline69 for tex2html_wrap_inline71 .

    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.

    推荐博客:http://www.cnblogs.com/devymex/archive/2010/08/09/1795391.html

    解题思路:

    计算几何类型的题目。需要用到三个基本算法,一是求凸包,二是判断点在多边形内,三是求多边形面积(题目中已给出)。关于凸包算法请详见Graham's Scan法。判断点在多边形内的算法有很多种,这里用到的是外积法:设待判断的点为p,逆时针或顺时针遍例多边形的每个点vn,将两个向量<p, vn>和<vn, vn + 1>做外积。如果对于多边形上所有的点,外积的符号都相同(顺时针为负,逆时针为正),则可断定p在多边形内。外积出现0,则表示p在边上,否则在多边形外。

    算法的思路很直接,实现也很简单,关键是这道题的测试数据太扯蛋了,让我郁闷了很久。题目中并未说明导弹打在墙上怎么办,只是说“... whithin the wall ...”。根据测试结果来看,打在墙上和打在据点上都要算打中。题目中还提到国家不会互相重叠“... kingdoms do not overlap.”,但测试表明数据里确有重叠的情况,因此在导弹击中后一定要跳出循环,否则会出现一弹多击的情况。

    给你N个王国,求下凸包,再求面积。给你一些炮弹,问炮弹炸掉的面积。(一个炮弹炸的话,整个王国都被炸了)。

    直接求凸包后,求出各个王国的面积,然后判断炮弹在哪个王国里,这个直接用判断点是否在多边形内。

    参考代码:

      1 #include<bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 const int MAX = 110;
      6 struct point{ double x,y;};        //
      7 struct polygon{ point c[MAX],a; double area; int n;};
      8 struct segment{ point a,b;};        // 线段 
      9 const double eps = 1e-6;
     10 bool dy(double x,double y)    {    return x > y + eps;}    // x > y 
     11 bool xy(double x,double y)    {    return x < y - eps;}    // x < y 
     12 bool dyd(double x,double y)    {     return x > y - eps;}    // x >= y 
     13 bool xyd(double x,double y)    {    return x < y + eps;}     // x <= y 
     14 bool dd(double x,double y)     {    return fabs( x - y ) < eps;}  // x == y
     15 polygon king[MAX];
     16 point c[MAX];
     17 double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 
     18 {
     19     return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
     20 }
     21 double disp2p(point a,point b) 
     22 {
     23     return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
     24 }
     25 double area_polygon(point p[],int n)
     26 {
     27     double s = 0.0;
     28     for(int i=0; i<n; i++)
     29         s += p[(i+1)%n].y * p[i].x - p[(i+1)%n].x * p[i].y;
     30     return fabs(s)/2.0;
     31 }
     32 bool cmp(point a,point b)  // 排序   
     33 {  
     34     double len = crossProduct(c[0],a,b);  
     35     if( dd(len,0.0) )  
     36         return xy(disp2p(c[0],a),disp2p(c[0],b));  
     37     return xy(len,0.0);  
     38 }
     39 bool onSegment(point a, point b, point c)
     40 {
     41     double maxx = max(a.x,b.x);
     42     double maxy = max(a.y,b.y);
     43     double minx = min(a.x,b.x);
     44     double miny = min(a.y,b.y);
     45     if( dd(crossProduct(a,b,c),0.0) && dyd(c.x,minx) && xyd(c.x,maxx) && dyd(c.y,miny) && xyd(c.y,maxy) )
     46         return true;
     47     return false;
     48 }
     49 bool segIntersect(point p1,point p2, point p3, point p4) 
     50 {
     51     double d1 = crossProduct(p3,p4,p1);
     52     double d2 = crossProduct(p3,p4,p2);
     53     double d3 = crossProduct(p1,p2,p3);
     54     double d4 = crossProduct(p1,p2,p4);
     55     if( xy(d1 * d2,0.0) && xy(d3 * d4,0.0) )    return true;
     56     if( dd(d1,0.0) && onSegment(p3,p4,p1) )        return true;//如果不判端点相交,则下面这四句话不需要 
     57     if( dd(d2,0.0) && onSegment(p3,p4,p2) )        return true;
     58     if( dd(d3,0.0) && onSegment(p1,p2,p3) )        return true;
     59     if( dd(d4,0.0) && onSegment(p1,p2,p4) )        return true;
     60     return false;
     61 }
     62 bool point_inPolygon(point pot,point p[],int n) 
     63 {
     64     int count = 0;
     65     segment l;
     66     l.a = pot;
     67     l.b.x = 1e10*1.0;
     68     l.b.y = pot.y;
     69     p[n] = p[0];
     70     for(int i=0; i<n; i++)
     71     {
     72         if( onSegment(p[i],p[i+1],pot) )
     73             return true;
     74         if( !dd(p[i].y,p[i+1].y) )
     75         {
     76             int tmp = -1;
     77             if( onSegment(l.a,l.b,p[i]) )
     78                 tmp = i;
     79             else
     80                 if( onSegment(l.a,l.b,p[i+1]) )
     81                     tmp = i+1;
     82             if( tmp != -1 && dd(p[tmp].y,max(p[i].y,p[i+1].y)) )
     83                 count++;
     84             else
     85                 if( tmp == -1 && segIntersect(p[i],p[i+1],l.a,l.b) )
     86                     count++;
     87         }
     88     }
     89     if( count % 2 == 1 )
     90         return true;
     91     return false;
     92 } 
     93 point stk[MAX];
     94 int top;
     95 double Graham(int n)
     96 {
     97     int tmp = 0;  
     98     for(int i=1; i<n; i++)
     99         if( xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y) )
    100             tmp = i;
    101     swap(c[0],c[tmp]);
    102     sort(c+1,c+n,cmp);
    103     stk[0] = c[0]; stk[1] = c[1];
    104     top = 1;
    105     for(int i=2; i<n; i++)
    106     {
    107         while( xyd( crossProduct(stk[top],stk[top-1],c[i]), 0.0 ) && top >= 1 )
    108             top--;
    109         stk[++top] = c[i];
    110     }
    111     return area_polygon(stk,top+1);
    112 }
    113 int main()
    114 {
    115     int n;
    116     int i = 0;
    117     double x,y;
    118     while( ~scanf("%d",&n) && n != -1 )
    119     {
    120         king[i].n = n;
    121         for(int k=0; k<n; k++)
    122             scanf("%lf %lf",&king[i].c[k].x,&king[i].c[k].y);
    123         king[i].a = king[i].c[0];
    124         i++;
    125     }
    126     
    127     double sum = 0.0;
    128     for(int k=0; k<i; k++)
    129     {
    130         memcpy(c,king[k].c,sizeof(king[k].c));
    131         king[k].area = Graham(king[k].n);
    132         memcpy(king[k].c,stk,sizeof(stk));
    133         king[k].n = top+1;
    134     }
    135     point pot;
    136     bool die[MAX];
    137     memset(die,false,sizeof(die));
    138     while( ~scanf("%lf %lf",&pot.x,&pot.y) )
    139     {
    140         for(int k=0; k<i; k++)
    141             if( point_inPolygon(pot,king[k].c,king[k].n) && !die[k] )
    142             {
    143                 die[k] = true;
    144                 sum += king[k].area;
    145             }
    146     }
    147     
    148     printf("%.2lf
    ",sum);
    149 return 0;
    150 }

    参考代码2:

     1 #include <algorithm>
     2 #include <functional>
     3 #include <iomanip>
     4 #include <iostream>
     5 #include <vector>
     6 #include <math.h>
     7 
     8 using namespace std;
     9 
    10 struct POINT {
    11     int x; int y;
    12     bool operator==(const POINT &other) {
    13         return (x == other.x && y == other.y);
    14     }
    15 } ptBase;
    16 
    17 typedef vector<POINT> PTARRAY;
    18 
    19 bool CompareAngle(POINT pt1, POINT pt2) {
    20     pt1.x -= ptBase.x, pt1.y -= ptBase.y;
    21     pt2.x -= ptBase.x, pt2.y -= ptBase.y;
    22     return (pt1.x / sqrt((float)(pt1.x * pt1.x + pt1.y * pt1.y)) <
    23         pt2.x / sqrt((float)(pt2.x * pt2.x + pt2.y * pt2.y)));
    24 }
    25 
    26 void CalcConvexHull(PTARRAY &vecSrc, PTARRAY &vecCH) {
    27     ptBase = vecSrc.back();
    28     sort(vecSrc.begin(), vecSrc.end() - 1, &CompareAngle);
    29     vecCH.push_back(ptBase);
    30     vecCH.push_back(vecSrc.front());
    31     POINT ptLastVec = { vecCH.back().x - ptBase.x,
    32         vecCH.back().y - ptBase.y };
    33     PTARRAY::iterator i = vecSrc.begin();
    34     for (++i; i != vecSrc.end() - 1; ++i) {
    35         POINT ptCurVec = { i->x - vecCH.back().x, i->y - vecCH.back().y };
    36         while (ptCurVec.x * ptLastVec.y - ptCurVec.y * ptLastVec.x < 0) {
    37             vecCH.pop_back();
    38             ptCurVec.x = i->x - vecCH.back().x;
    39             ptCurVec.y = i->y - vecCH.back().y;
    40             ptLastVec.x = vecCH.back().x - (vecCH.end() - 2)->x;
    41             ptLastVec.y = vecCH.back().y - (vecCH.end() - 2)->y;
    42         }
    43         vecCH.push_back(*i);
    44         ptLastVec = ptCurVec;
    45     }
    46     vecCH.push_back(vecCH.front());
    47 }
    48 
    49 int CalcArea(PTARRAY &vecCH) {
    50     int nArea = 0;
    51     for (PTARRAY::iterator i = vecCH.begin(); i != vecCH.end() - 1; ++i) {
    52         nArea += (i + 1)->x * i->y - i->x * (i + 1)->y;
    53     }
    54     return nArea;
    55 }
    56 
    57 bool PointInConvexHull(POINT pt, PTARRAY &vecCH) {
    58     for (PTARRAY::iterator i = vecCH.begin(); i != vecCH.end() - 1; ++i) {
    59         int nX1 = pt.x - i->x, nY1 = pt.y - i->y;
    60         int nX2 = (i + 1)->x - i->x, nY2 = (i + 1)->y - i->y;
    61         if (nX1 * nY2 - nY1 * nX2 < 0) {
    62             return false;
    63         }
    64     }
    65     return true;
    66 }
    67 
    68 int main(void) {
    69     vector<PTARRAY> vecKingdom;
    70     POINT ptIn;
    71     int aFlag[100] = {0}, nAreaSum = 0;
    72     for (int nPtCnt; cin >> nPtCnt && nPtCnt >= 1;) {
    73         PTARRAY vecSrc, vecCH;
    74         cin >> ptIn.x >> ptIn.y;
    75         vecSrc.push_back(ptIn);
    76         for (; --nPtCnt != 0;) {
    77             cin >> ptIn.x >> ptIn.y;
    78             POINT &ptMin = vecSrc.back();
    79             vecSrc.insert(vecSrc.end() - (ptIn.y > ptMin.y ||
    80                 (ptIn.y == ptMin.y && ptIn.x > ptMin.x)), ptIn);
    81         }
    82         CalcConvexHull(vecSrc, vecCH);
    83         vecKingdom.push_back(vecCH);
    84     }
    85     while (cin >> ptIn.x >> ptIn.y) {
    86         vector<PTARRAY>::iterator i = vecKingdom.begin();
    87         for (int k = 0; i != vecKingdom.end(); ++i, ++k) {
    88             if (PointInConvexHull(ptIn, *i) && aFlag[k] != 1) {
    89                 nAreaSum += CalcArea(*i);
    90                 aFlag[k] = 1;
    91                 break;
    92             }
    93         }
    94     }
    95     cout << setiosflags(ios::fixed) << setprecision(2);
    96     cout << (float)nAreaSum / 2.0f << endl;
    97     return 0;
    98 }
  • 相关阅读:
    (转)C#特性详解
    PHP MSSQL数据操作PDO API
    DWR
    配置JAVA的环境变量
    关​于​h​i​b​e​r​n​a​t​e​中​双​向​外​键​关​联​o​n​e​-​t​o​-​o​n​e​的​p​r​o​p​e​r​t​y​-​r​e​f​=​的​问​题(转)
    Hibernate 二级缓存 总结整理(转)
    EhCache 分布式缓存/缓存集群(转)
    MySQL分区表(转)
    通用权限设计
    (转)关于Struts 2 拦截器参数丢失问题
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/5049517.html
Copyright © 2020-2023  润新知