HDU 1616 compute geometry convex hull

Source: Internet
Author: User

Test instructions is a world with many countries, each with n buildings, including a power station and N-1 buildings, all of which are surrounded by a convex hull that is the size of the country. A missile that explodes within a country can cause the country to power down.

Step 1: Find the convex hull of each country (I use the horizontal sort is all kinds of pits, change the fork by the sort before, mainly is the back area of the time need this fork to sort information).

Step 2: Determine whether each missile is within the range of the country.

Step 3: Find out the area of all countries with blackout.

is to calculate the geometry of the comprehensive simulation water problem, pit point is to be careful (qaq| | The problem of writing computational geometry is to be careful.

Portal: http://acm.hdu.edu.cn/showproblem.php?pid=1616

#include <stdio.h> #include <math.h> #include <string.h> #include <algorithm> #include < Iostream> #define MAXN 105#define eps 1e-8using namespace std;struct Node {double x, y;};    struct king{Node B[MAXN];    Double area;    int mark; int Lenk;};            King a[maxn];int lt;double FX, Fy;bool cmpconvex1 (Node a1,node A2) {if ((A1.X-FX) * (a2.y-fy) = = (a1.y-fy) * (A2.X-FX)) {            if (a1.y = = a2.y) return a1.x < a2.x;    else return a1.y < a2.y; } return (A1.X-FX) * (a2.y-fy) > (a1.y-fy) * (A2.X-FX);} Double dot (double x1,double y1,double x2,double y2) {return x1 * y2-x2 * y1;} Double Cross (Node a1,node a2,node A3) {return (a2.x-a1.x) * (A3.Y-A2.Y)-(a3.x-a2.x) * (A2.Y-A1.Y);} int Convexhull (Node * p,node * ans,int t) {for (int i=1;i<t;i++) {if (P[I].Y==P[0].Y) {if (p[i].x<        p[0].x) Swap (p[i],p[0]);        } else if (P[I].Y&LT;P[0].Y) {swap (p[i],p[0]); }} FX = p[0].x;    FY = p[0].y;    Sort (P,P+T,CMPCONVEX1);    int k=0;        for (int j=0;j<t;j++) {while (K>=2&&cross (Ans[k-2],ans[k-1],p[j]) <0) {k--;    } ans[k++] = P[j]; } return k;}    Double Getarea (Node * ans,int lenc) {double area = 0;    for (int. i=0;i<lenc;i++) {area + = dot (ans[i].x,ans[i].y,ans[(i+1)%lenc].x,ans[(i+1)%lenc].y); } return area;}    int Is_inside (double x1,double y1,king A1) {Double Temparea = 0; for (int i=0;i<a1.lenk;i++) {Temparea + = Fabs (dot (a1.b[i].x-x1,a1.b[i].y-y1, a1.b[(i+1)%a1    . lenk].x-x1,a1.b[(i+1)%a1.lenk].y-y1));    }//printf ("%lf%lf\n", Temparea,a1.area);    if (Fabs (Temparea-a1.area) <eps) return 1; else return 0;}    void input () {int n;    lt = 0;    memset (A,0,sizeof (a));        while (scanf ("%d", &n), n+1) {Node TEMP[MAXN];        Node ANS[MAXN];    for (int i=0;i<n;i++) {scanf ("%lf%lf", &temp[i].x,&temp[i].y);    } int lenc = Convexhull (temp,ans,n);            for (int i=0;i<lenc;i++) {a[lt].b[i].x = ans[i].x;        A[lt].b[i].y = ANS[I].Y;        } A[lt].area = Getarea (Ans,lenc);        A[lt].lenk = Lenc;    lt++;    } double x1,y1;                while (~scanf ("%lf%lf", &x1,&y1)) {for (int i=0;i<lt;i++) {if (Is_inside (X1,y1,a[i])) {            A[i].mark = 1;    }}} Double Zans = 0;    for (int i=0;i<lt;i++) {if (A[i].mark) Zans + = A[i].area; } printf ("%.2f\n", ZANS/2);}    void File () {freopen ("a.in", "R", stdin); Freopen ("A.out", "W", stdin);}    int main (void) {//file ();    Input (); return 0;}


HDU 1616 compute geometry convex hull

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.