标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1037 Accepted Submission(s): 175
题目大意:
题目的意思就是说,给你一个二维平面图,然后,在(0,0)点有一个发射炮,有一个n边形的飞机,问你有没有可能这个炮弹打到飞机。飞机以v=v0做匀速运动,炮弹是具有初速度为b,重力加速度为g的上抛运动。。。。如果能够打到飞机的话,就输出在某一个时刻
解题思路:
这道题当时现场赛A出来的人不多,有可能是因为精度问题吧,但是,说实话,这个题的难度并不大,如果考虑到了相对坐标变化的思想,就很容易能够写出来了,
我们假设飞机静止不动,那么,这个导弹的飞行曲线就是一个类似于 平抛运动的物体了x=-v*t,y=0.5*g*t*t;
有关点在多边形内部的判断用的是LRJ的模板。
代码:
# include<cstdio> # include<iostream> # include<vector> # include<cmath> using namespace std; const double PI=acos(-1.0); const double eps=1e-10; int dcmp(double x) { if(fabs(x)<eps)return 0; return x<0?-1:1; } struct Point { double x,y; Point(){} Point(double x,double y):x(x),y(y){} Point operator+(const Point&p){return Point(x+p.x,y+p.y);} Point operator-(const Point&p){return Point(x-p.x,y-p.y);} Point operator*(double p){return Point(x*p,y*p);} Point operator/(double p){return Point(x/p,y/p);} }; typedef Point Vector; double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;} double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;} double Length(Vector a){return sqrt(Dot(a,a));} double Angle(Vector a,Vector b){return acos(Dot(a,b)/Length(a)/Length(b));} bool OnSegment(Point p,Point a,Point b) { if(dcmp(Cross(p-a,p-b)))return 0; return dcmp(a.x-p.x)*dcmp(b.x-p.x)<=0&&dcmp(a.y-p.y)*dcmp(b.y-p.y)<=0; } typedef vector<Point> Polygon; int isPointInPolygon(Point p,Polygon poly) { int wn=0; int n=poly.size(); for(int i=0;i<n;i++) { if(OnSegment(p,poly[i],poly[(i+1)%n]))return 0; 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--; } return wn; } int main(void) { int n; double v,b,g; while( scanf("%lf%lf%lf",&v,&b,&g)!=EOF ) { if(v==0&&b==0&&g==0) break; scanf("%d",&n); Polygon p; double max_y = 0.0,x,y; for( int i = 0;i < n;i++ ) { scanf("%lf%lf",&x,&y); max_y =max(max_y,y); p.push_back(Point(x,y)); } int ok=0; double T=dcmp(g)?2.0*b/g:max_y/b; for( double t = 0.0;t <= T;t += 0.001 ) { Point tmp(-v*t,b*t-0.5*g*t*t); if(isPointInPolygon(tmp,p)) { printf("%.2lf\n",t); ok=1; break; } } if(!ok) puts("Miss!"); } }
HDU 4458 Shoot the Airplane(计算几何 判断点是否在n边形内)
标签:
原文地址:http://www.cnblogs.com/wikioibai/p/4772073.html