Ultraviolet A, OJ, 109-Scud busters (SCUD blockbuster)

Source: Internet
Author: User

Time Limit: 3.000 seconds

 

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 × 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 Wals 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 writing 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 wallsurrounding 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 (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 by 500 grid. all coordinates will be integers between 0 and 500 aggressive. 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 configuricity after all missile attacks have been processed. The number shocould 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

 

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

Where the X, Y coordinates of Vi = (XI, Yi); the edges of the polygon are from VI to vi + 1 for I = 0... n-1.

If the points describing the polygon are given in a counterclockwise ction, the value of a will be positive, and if the points of the polygon are listed in a clockwise ction, the value of a will be negative.

 

Analysis

Calculate the questions of geometric type. Three basics are requiredAlgorithmThe first is to find the convex hull, the second is to judge the point in the polygon, and the third is to calculate the Polygon Area (the problem has been given ). For more information about the convex hull algorithm, see the Graham's scan method. There are many algorithms for determining points in a polygon. Here we use the outer product method: Set the point to be judged to be P, every point of the polygon in the example of VN in a counterclockwise or clockwise manner, perform the outer product of the two vectors <p, vn> and <VN, vn + 1>. If all vertices on the polygon have the same outer product symbol (clockwise negative, clockwise positive), P can be determined within the polygon. If the outer product is 0, P is on the edge; otherwise, P is on the outside of the polygon.

The idea of the algorithm is very direct, and the implementation is very simple. The key is that the test data for this question is too boring, so I have been depressed for a long time. The question does not explain what to do when a missile hits the wall. It just says "... whithin the wall ...". According to the test results, hitting the wall and the data point is counted. The topic also mentions that countries do not overlap "... kingdoms do not overlap. ", but the test shows that there is indeed overlap in the Data. Therefore, you must jump out of the loop after the missile hits. Otherwise, multiple hits may occur.

 

 

Solution
# Include <algorithm> # include <functional> # include <iomanip> # include <iostream> # include <vector> # include <math. h> using namespace STD; struct point {int X; int y; bool operator = (const point & Other) {return (x = Other. X & Y = Other. y) ;}} ptbase; typedef vector <point> ptarray; bool compareangle (point pt1, point pt2) {pt1.x-= ptbase. x, pt1.y-= ptbase. y; pt2.x-= ptbase. x, pt2.y-= ptbase. y; Return (pt1. X/SQRT (float) (pt1.x * pt1.x + pt1.y * pt1.y) <pt2.x/SQRT (float) (pt2.x * pt2.x + pt2.y * pt2.y )));} void calcconvexhull (ptarray & vecsrc, ptarray & vecch) {ptbase = vecsrc. back (); sort (vecsrc. begin (), vecsrc. end ()-1, & compareangle); vecch. push_back (ptbase); vecch. push_back (vecsrc. front (); point ptlastvec = {vecch. back (). x-ptbase. x, vecch. back (). y-ptbase. y}; ptarray: iterator I = vecsrc. begin (); For (++ I; I! = Vecsrc. end ()-1; ++ I) {point ptcurvec = {I-> X-vecch. back (). x, I-> Y-vecch. back (). y}; while (ptcurvec. x * ptlastvec. y-ptcurvec. y * ptlastvec. x <0) {vecch. pop_back (); ptcurvec. X = I-> X-vecch. back (). x; ptcurvec. y = I-> Y-vecch. back (). y; ptlastvec. X = vecch. back (). x-(vecch. end ()-2)-> X; ptlastvec. y = vecch. back (). y-(vecch. end ()-2)-> Y;} vecch. push_back (* I); ptlastvec = ptcurvec;} vecch. push _ Back (vecch. Front ();} int calcarea (ptarray & vecch) {int narea = 0; For (ptarray: iterator I = vecch. Begin (); I! = Vecch. end ()-1; ++ I) {narea + = (I + 1)-> X * I-> Y-I-> X * (I + 1) -> Y;} return narea;} bool pointinconvexhull (point PT, ptarray & vecch) {for (ptarray: iterator I = vecch. begin (); I! = Vecch. end ()-1; ++ I) {int Nx1 = pt. x-I-> X, NY1 = pt. y-I-> Y; int nx2 = (I + 1)-> X-I-> X, ny2 = (I + 1)-> Y-I-> Y; if (Nx1 * ny2-NY1 * nx2 <0) {return false ;}} return true;} int main (void) {vector <ptarray> veckingdom; point ptin; int aflag [100] = {0}, nareasum = 0; For (INT nptcnt; CIN >>> nptcnt & nptcnt >=1;) {ptarray vecsrc, vecch; cin> ptin. x> ptin. y; vecsrc. push_back (ptin); For (; -- nptcnt! = 0;) {CIN> ptin. x> ptin. y; point & ptmin = vecsrc. back (); vecsrc. insert (vecsrc. end ()-(ptin. y> ptmin. Y | (ptin. y = ptmin. Y & ptin. x> ptmin. x), ptin);} calcconvexhull (vecsrc, vecch); veckingdom. push_back (vecch);} while (CIN> ptin. x> ptin. y) {vector <ptarray>: iterator I = veckingdom. begin (); For (int K = 0; I! = Veckingdom. End (); ++ I, ++ K) {If (pointinconvexhull (ptin, * I) & aflag [k]! = 1) {nareasum + = calcarea (* I); aflag [k] = 1; break ;}} cout <setiosflags (IOs: fixed) <setprecision (2); cout <(float) nareasum/2.0f <Endl; return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.