首页 > 代码库 > HDU 4445 Crazy Tank --枚举

HDU 4445 Crazy Tank --枚举

题意: n个物体从高H处以相同角度抛下,有各自的初速度,下面[L1,R1]是敌方坦克的范围,[L2,R2]是友方坦克,问从某个角度抛出,在没有一个炮弹碰到友方坦克的情况下,最多的碰到敌方坦克的炮弹数。

解法: 枚举角度,将pi/2分成1000份,然后枚举,通过方程 v*sin(theta)*t - 1/2*g*t^2 = -H 解出t,然后 x = v*cos(theta)*t算出水平距离,直接统计即可。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#define pi acos(-1.0)#define eps 1e-8using namespace std;#define N 207#define g 9.8double V[N];double H;int sgn(double x){    if(x > eps) return 1;    if(x < -eps) return -1;    return 0;}double calc(double theta,double v){    double up = v*sin(theta) + sqrt(v*v*sin(theta)*sin(theta)+2.0*g*H);    double down = g;    return v*cos(theta)*up/down;}int main(){    double L1,R1,L2,R2;    int n,i,j;    while(scanf("%d",&n)!=EOF && n)    {        scanf("%lf%lf%lf%lf%lf",&H,&L1,&R1,&L2,&R2);        if(sgn(L1-L2) == 0 && sgn(R1-R2) == 0) { puts("0"); continue; }        for(i=1;i<=n;i++) scanf("%lf",&V[i]);        double delta = pi*0.001;        int Maxi = 0;        for(i=0;i<=1000;i++)        {            double theta = delta*i - pi/2.0;            int cnt = 0;            for(j=1;j<=n;j++)            {                double x = calc(theta,V[j]);                if(sgn(x-L2) >= 0 && sgn(x-R2) <= 0)                {                    cnt = 0;                    break;                }                if(sgn(x-L1) >= 0 && sgn(R1-x) >= 0)                    cnt++;            }            Maxi = max(Maxi,cnt);        }        cout<<Maxi<<endl;    }    return 0;}
View Code

 

HDU 4445 Crazy Tank --枚举