#include<iostream>
#include<cmath>
using namespace std;
/*
* AUTHOR : benbendog
* DATE : 2011/2/5
* PROBLEM: given n players and their value(x,y,speed) and
b balls (a,b,c,d,e,f,g)
z = at^2+bt+c , x = d*t+e , y = f*t +g
find if one of the player can catch the ball (when z <=0)
if one of x,y < 0 , print foul
if no one can catch, print safe
otherwise print caught
* SOLUTION: first find t, when z <=0 , use t1 = (-b-delta)/(2a);
t2 = (-b+delta)/(2a)
when a >0 , values in section [t1,t2] is >= 0
Thus, test floor(t1) >0 then ceil(t2) > 0, because t should be >0
if floor(t1) > 0 return it , otherwise return ceil(t2)
when a <0 , values in section[t2,t1] is <=0
Thus, test ceil(t2) > 0 then floor(t1) >0
if ceil(t2) > 0 return it , otherwise return floor(t1)
the rest is trivial
when a = 0
if b > 0 , return floor(-c/b);
otherwise, return ceil(-c/b);
* CAUTION: at first glance, I wonder if the testcase would contain
the solution t < 0. But it seems not to have it.
*/
struct player
{
int x,y,speed;
};
struct ball
{
int a,b,c,d,e,f,g;
};
double dist(const player & p,int x,int y);
int touchdownTime(const ball &b);
int main()
{
int np,nb,i,t,x,y,j;
double d;
while(scanf("%*[^=]=%d",&np)==1)
{
player p[np];
ball b;
for(i = 0 ;i < np ; ++i)
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].speed);
scanf("%*[^=]=%d",&nb);
for(i = 0 ;i < nb; ++i)
{
scanf("%d%d%d%d%d%d%d",&b.a,&b.b,&b.c,&b.d,&b.e,&b.f,&b.g);
t = touchdownTime(b);
x = b.d*t+b.e;
y = b.f*t+b.g;
if(x <0 || y<0)printf("Ball %d was foul at (%d,%d)\n",i+1,x,y);
else
{
for(j = 0 ;j < np; ++j)
{
d = dist(p[j],x,y);
if(d <= p[j].speed*t)
{
printf("Ball %d was caught at (%d,%d)\n",i+1,x,y);
break;
}
}
if(j==np)
printf("Ball %d was safe at (%d,%d)\n",i+1,x,y);
}
}
}
return 0;
}
int touchdownTime(const ball &ba)
{
double sol[2];
int a,b,c;
double delta;
a = ba.a ; b= ba.b ; c = ba.c;
delta = sqrt(b*b-4*a*c);
if(a >0)
{
sol[0] = (-b-delta)/(2*a);
sol[1] = (-b+delta)/(2*a);
a = ceil(sol[0]);
b = floor(sol[1]);
if(a >0)return a;
else if(b>0)return b;
else return 0;
}
else if(a<0)
{
sol[1] = (-b-delta)/(2*a);
sol[0] = (-b+delta)/(2*a);
//cout<<"left = "<<sol[0]<<endl;
//cout<<"right= "<<sol[1]<<endl;
a = floor(sol[0]);
b = ceil(sol[1]);
if(a > 0)return a;
else if(b>0)return b;
else return 0;
}
else
{
if(b <0)return ceil((-c)/b);
else if(b >0)return floor((-c)/b);
else return 0;
}
}
double dist(const player & p,int x,int y)
{
x = p.x - x;
y = p.y - y;
return sqrt(x*x + y*y);
}
- Feb 05 Sat 2011 15:20
UVA 10357
close
全站熱搜
留言列表