hdu4458Shoot the Airplane+计算几何

Problem Description
XXX is playing an interesting game which is based on a 2D plane. In this game, he is required to shoot an airplane. The airplane flies horizontally. The shape of it can be regarded as a simple polygon. Player has to shoot at point (0, 0) upward vertically. Because the bullet is very small, it can be regarded as a point. The airplane is hit only if the bullet goes into the airplane. The airplane is not hit if the bullet only touches the edge of the airplane. The gravity should be considered. The acceleration of gravity is g and its direction is downward vertically. So the speed of the bullet may be change during flying. But the speed of airplane is constant because it has engines. XXX wants to know the time it takes the bullet to hit the airplane after shooting.

Input
There are multiple cases.
In each case, there are three integers v (-10<= v <= 10), b (1 <= b <= 10), g (0 <= g <= 10) in the first line. v denotes the speed of airplane. The flying direction is from left to right if v is positive and the direction is from right to left if v is negative. b denotes the initial speed of bullet. g is the acceleration of gravity. Then the input will describe the position of the airplane when XXX shoots. In the second line, there is one integer n (3 <= n <= 20) which represents the number of vertexes of the airplane (polygon). In each of the next n lines, there are two integers x (-100 <= x <= 100), y (0 < y<= 100) which give the position of a vertex in order. There is a blank line after each case. The input ends with 0 0 0.

Output
If the airplane is hit, then output the time it takes the bullet to hit it after shooting in one line. The answer should be rounded to 2 digits after decimal point. If the airplane is not hit, then output “Miss!” in one line.

Sample Input

-10 10 2
9
6 9
10 9
10 16
25 16
25 20
10 20
10 27
6 27
-10 18

-10 10 2
9
6 9
10 9
10 16
20 16
20 20
10 20
10 27
6 27
-10 18

0 0 0

Sample Output

2.00
Miss!

Source
2012 Asia Hangzhou Regional Contest
题意:多边形的飞机水平飞行。从原点有炮弹垂直向上打出,问能不能打到飞机。
解法:可以将飞机看作静止的,相对于炮弹是抛物线的飞行,直接枚举时间,计算出炮弹的坐标,然后判断点是否在多边形内。。。(做的时候g==0从脑海中一闪而过。然后T死了。。)当g为0的时候,就是匀速直线运动,最大时间就是飞过最高点的时间。。

#include<bits/stdc++.h>
using namespace std;
const int maxn=100;
const double eps=1e-8;
struct point{
    double x,y;
    point(){};
    point (double x,double y):x(x),y(y){};
    friend point operator - (const point &a,const point &b){
        return point(a.x-b.x,a.y-b.y);
    }
};
int dcmp(double k){
    return k<-eps?-1:k>eps?1:0;
}
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;
}
struct Polygon{
    int n;
    point a[maxn];
    Polygon(){};
    int Point_in(point t){
        int num=0;
        a[n]=a[0];
        for(int i=0;i<n;i++){
            if(PointOnSegment(t,a[i],a[i+1])) return 2;
            int k=dcmp(det(a[i+1]-a[i],t-a[i]));
            int d1=dcmp(a[i].y-t.y);
            int d2=dcmp(a[i+1].y-t.y);
            if(k>0&&d1<=0&&d2>0) num++;
            if(k<0&&d2<=0&&d1>0) num--;
        }
        return num!=0;
    }
};
Polygon plane;
int main(void) {

    int v,b,g;
    while(scanf("%d %d %d",&v,&b,&g)!=EOF){
        if(v==0&&b==0&&g==0) break;
        scanf("%d",&plane.n);

        double tem=0;
        for(int i=0;i<plane.n;i++){
            scanf("%lf %lf",&plane.a[i].x,&plane.a[i].y);
            //if(plane.a[i].y<d.y) d=point(plane.a[i].x,plane.a[i].y);
            tem=max(tem,plane.a[i].y);
        }
        double maxt;
        if(g<eps) maxt=tem/b;//大概就可以认为是0了
        else maxt=2.0*b/g;
        bool flag=false;
        double t;
        for( t=0.0;t<=maxt;t+=0.001){
            point op;
            op.x=-v*t;
            op.y=b*t-g*t*t/2.0;
            if(plane.Point_in(op)==1){
                flag=true;
                break;
            }
        }
        if(flag)  printf("%.2f\n",t);
        else printf("Miss!\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值