HDU

思路:将飞机看成不动的,然后枚举时间看点是否在多边形内部。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std;

const int N = 20 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-10;

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) { }

};
typedef Point Vector;
int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}
Point operator + (Vector A, Vector B) {return Point(A.x + B.x, A.y + B.y);}
Point operator - (Vector A, Vector B) {return Point(A.x - B.x, A.y - B.y);}
Point operator * (Vector A, double p) {return Point(A.x * p, A.y * p);}
Point operator / (Vector A, double p) {return Point(A.x / p, A.y / p);}
bool operator < (const Vector &A, const Vector &B) {return A.y < B.y || (A.y == B.y && A.x < B.x);}
bool operator == (const Vector &A, const Point &B) {return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0;}
double Dot(Vector A, Vector B) {return A.x * B.x + A.y * B.y;}
double Length(Vector A) {return sqrt(Dot(A, A));}
double Angle(Vector A, Vector B) {return acos(Dot(A, B) / Length(A) / Length(B));}
double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;}
double Area2(Point A, Point B, Point C) {return Cross(B - A, C - A);}

double v, b, g;
Point p[N];
int n;

bool isPointOnSegment(const Point &p, const Point &a1, const Point &a2)
{
    if(dcmp(Cross(a1-p,a2-p))) return 0;
    else if(dcmp(p.x-min(a1.x,a2.x))>=0&&dcmp(p.x-max(a1.x,a2.x))<=0
            &&dcmp(p.y-min(a1.y,a2.y))>=0&&dcmp(p.y-max(a1.y,a2.y))<=0) return 1;
    else return 0;
}

int isPointInPolygon(Point p, Point *poly, int n) {
    int wn = 0;
    for(int i = 0; i < n; i++) {
        if(isPointOnSegment(p, poly[i], poly[(i+1)%n])) return -1; //在边界上
        int k = dcmp(Cross(poly[(i+1)%n]-poly[i], p-poly[i]));
        int d1 = dcmp(poly[i].y-p.y);
        int d2 = dcmp(poly[(i+1)%n].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; //外部
}

bool check(double t) {
    Point pos = Point(-v*t, 0.5*-g*t*t + b*t);
    if(isPointInPolygon(pos, p, n) == 1) return true;
    return false;
}

int main() {
    while(scanf("%lf%lf%lf", &v, &b, &g) != EOF) {
        if(v < eps && b < eps && g < eps) break;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        double ans = -1;
        for(double t = 0; t <= 100; t += 0.001) {
            if(check(t)) {
                ans = t;
                break;
            }
        }
        if(ans < 0) puts("Miss!");
        else printf("%.2f
", ans);
    }
    return 0;
}

/*
ŝ
*/
原文地址:https://www.cnblogs.com/CJLHY/p/9800736.html