点击打开hdu4458
思路:
这个题目是一个相对运动题目,另外竖直方向与水平方向运动分开判断。既然是相对运动那么可以把飞机看作不动,子弹向飞机飞去,然后暴力枚举,卡题目的精度。首先考虑时间,分两种情况g=0与g!=0,如果等于0那么就是匀速直线运动,子弹竖直方向会一直向上飞;而g!=0时要考虑重力作用,会做匀减速运动,会往上飞到最高点然后往下落,所以要考虑子弹是往上飞打中飞机还是落下时打中飞机。所以T可能为b/g*2或者ymax/b(匀速直线运动只考虑子弹在飞机范围内的运动)。如过公式不知道可以先去百度一下。
下面是代码:
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<cstring>
#include<stdlib.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define eps 1e-10
int n;
int dcmp(double x)
{if(fabs(x)<eps) return 0;return x<0?-1:1;
}struct point
{double x,y;
}p[30];//??point operator-(point a,point b)
{point t;t.x=a.x-b.x;t.y=a.y-b.y;return t;
}
typedef point Vector;double s(point a,point b,point c)
{double bax = b.x-a.x;double bay = b.y-a.y;double cax = c.x-a.x;double cay = c.y-a.y;return fabs(bax*cay-bay*cax)*0.5;
}double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}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; ///利用dcmp判断,避免使用Dot
}int judge1(point k)
{for(int i=0;i<=n;i++){if(OnSegment(k,p[i],p[(i+1)%n])) return 0;}return 1;
}
int judge2(point k)///判断是否在多边形里面,包括在边上,适用于所有多边形
{int i, j, c = 0;for (i = 0, j = n-1; i <n; j = i++) {if ( ((p[i].y>k.y) != (p[j].y>k.y)) &&(k.x < (p[j].x-p[i].x) * (k.y-p[i].y) / (p[j].y-p[i].y) + p[i].x) )c = !c;}return c;
}int main()
{int v,b,g;while(scanf("%d %d %d",&v,&b,&g)!=EOF){if(v==0&&b==0&&g==0) break;memset(p,0,sizeof(p));scanf("%d",&n);double xmax=-101,xmin=101,ymax=-101,ymin=101;for(int i=0;i<n;i++)///存点{scanf("%lf %lf",&p[i].x,&p[i].y);if(xmax<p[i].x) xmax=p[i].x;if(xmin>p[i].x) xmin=p[i].x;if(ymax<p[i].y) ymax=p[i].y;if(ymin>p[i].y) ymin=p[i].y;// p[i].x=p[i].x+v*t;}p[n]=p[0];if(v<0&&xmax<0){printf("Miss!\n");continue;}if(v>0&&xmin>0){printf("Miss!\n");continue;}int flag=0;point k;k.x=0,k.y=0;double kv=-v;double T=dcmp(g)?2.0*b/g:ymax/b;///两种情况// cout<<ymax<<endl;// cout<<T<<endl;int count=0;for(double i=0.0;i<=T;i+=0.001)///暴力枚举时间点{//count++;// cout<<132<<endl;k.x=kv*i;///Vx*Tk.y=b*i-(0.5*g*i*i);/// Vy*t-1/2gt^2//cout<<k.x<<" "<<k.y<<endl;if(judge2(k)&&judge1(k))///满足条件的点必须在多边形内部,不能在边上{flag=1;//cout<<12333333333333<<endl;printf("%.2lf\n",i);break;}if(flag) break;}//cout<<123<<endl;//cout<<count<<endl;if(!flag) printf("Miss!\n");}return 0;// p[0]=a{0,0},p[1]=b{2,2},p[2]=c{2,2};//cout<<judge(p,3)<<endl;
}