FZUACM Problem 1120 A Pilot in Danger!

#include <iostream>

#include <cmath>

#include <algorithm>

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

//2013-06-05 17:19:28 	Accepted 	1120 	GNU C++		0 ms 	856KB 	2352B 

const double eps = 1e-8;

int dcmp(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) {}

    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);

    }

};



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;

}



bool PointOnSegment(point p, point s, point t) {

    return dcmp(det(p-s, t-s))==0 && (dcmp(dot(p-s, p-t))<=0);

}

const int maxn = 40100;

//多边形的点顺时针,逆时针都可以。

struct polygon {

    int n;

    point a[maxn];

    //polygon() {}

    int Point_In(point t) {

        int num = 0, i, d1, d2, k;

        a[n] = a[0];

        for(i = 0; i < n; i++) {

            if(PointOnSegment(t, a[i], a[i+1])) return 2;

            k = dcmp(det(a[i+1]-a[i], t-a[i]));

            d1 = dcmp(a[i].y - t.y);

            d2 = dcmp(a[i+1].y - t.y);

            if(k>0 && d1<=0 && d2>0) num++;

            if(k<0 && d2<= 0 && d1>0) num--;

        }

        if(num!=0) return 1;//在多边形内。

        return 0;

    }

};



polygon v;

int main()

{

    int n;

    int p, q;

    int num = 0;

    while(scanf("%d", &n) != EOF) {

        if(n==0) break;

        v.n = n;

        for(int i = 0; i < n; i++) {

            scanf("%lf%lf", &v.a[i].x, &v.a[i].y);

        }

        scanf("%d%d", &p, &q);

        int secret = (p-1)*(q-1)/2;

        int res = v.Point_In(point(0, 0));

        printf("Pilot %d\n",  ++num);

        if(res==1) {

            printf("The pilot is in danger!\n");

            printf("The secret number is %d.\n", secret);

        } else {

            printf("The pilot is safe.\n");

        }

        cout << endl;

    }

}


你可能感兴趣的:(ACM)