2009腾讯创新技术大赛 Ball
Problem C: Ball
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 319 | Accepted: 87 |
Description
给出空间上两个运动的小球,球心坐标在分别是A(xa , ya , 0),B(xb , yb , 0),半径分别为Ra , Rb,速度分别为Va( vax, vay , 0), Vb( vbx , vby, 0)。判断两个小球是否会碰撞,若会碰撞, 输出首次碰撞时的时刻和两个小球的坐标;若不会碰撞,输出“Impossible”(球心和速度的z坐标恒为0,可将本题视为只是平面上的运动。初始时刻为0,若初始时刻小球贴在一起,视为首次碰撞)。
Input
第一行:一个整数T,(T ≤ 30),表示下面有T组数据。
接下来,每两行组成一组数据,首行包含5个实数,用空格隔开,依次是xa , ya , vax, vay , Ra,下面一行也包含5个实数,依次是xa , ya , vbx , vby, Rb。每组数据之间有一个空行。
接下来,每两行组成一组数据,首行包含5个实数,用空格隔开,依次是xa , ya , vax, vay , Ra,下面一行也包含5个实数,依次是xa , ya , vbx , vby, Rb。每组数据之间有一个空行。
Output
对于每组数据,如果两个小球会碰撞,输出首次碰撞时的时刻t,和两个小球的坐标xap , yap , xbp , ybp ,用空格隔开,保留三位小数。如果不能,输出“Impossible”。
Sample Input
3 100 200 0 0 55 100 100 0 0 45 131 123 45 2 43 454 230 0 -5 35 100 100 1 1 31 200 200 2 2 23
Sample Output
0.000 100.000 200.000 100.000 100.000 6.179 409.053 135.358 454.000 199.105 Impossible
[Submit]
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double xa , ya , xb , yb;
double vax, vay , ra , rb , vbx , vby;
double x , y , vx , vy , r;
double dd , sdd , a , b , c;
double t1 , t2;
int t;
cin >> t;
while (t --)
{
cin >> xa >> ya >> vax >> vay >> ra;
cin >> xb >> yb >> vbx >> vby >> rb;
x = xa - xb;
vx = vax - vbx;
y = ya - yb;
vy = vay - vby;
r = ra + rb;
if (x * x + y * y == r * r)
{
printf ("%.3f %.3f %.3f %.3f %.3f\n" , 0.000 , xa , ya , xb , yb);
continue;
}
a = vx * vx + vy * vy;
b = 2 * (vx * x + y * vy);
c = x * x + y * y - r * r;
double temp = -1;
if (a == 0)
{
temp = (- c )/ b;
if (temp <= 0)
{
cout << "Impossible" << endl;
continue;
}
else
{
printf ("%.3f %.3f %.3f %.3f %.3f\n" , temp , xa + vax * temp , ya + vay * temp , xb + vbx * temp , yb + vby * temp);
}
}
dd = b * b - 4 * a * c ;
if (dd < 0)
{
cout << "Impossible" << endl;
continue;
}
sdd = sqrt (dd);
t1 = ((-b) + sdd) / (2 * a);
t2 = -(b + sdd) / (2 * a);
if (t1 > 0)
{
temp = t1;
if (t2 > 0 && temp > t2)
temp = t2;
}
else
temp = t2;
if (temp < 0)
{
cout << "Impossible" << endl;
continue;
}
printf ("%.3f %.3f %.3f %.3f %.3f\n" , temp , xa + vax * temp , ya + vay * temp , xb + vbx * temp , yb + vby * temp);
}
return 0;
}
#include <cmath>
using namespace std;
int main()
{
double xa , ya , xb , yb;
double vax, vay , ra , rb , vbx , vby;
double x , y , vx , vy , r;
double dd , sdd , a , b , c;
double t1 , t2;
int t;
cin >> t;
while (t --)
{
cin >> xa >> ya >> vax >> vay >> ra;
cin >> xb >> yb >> vbx >> vby >> rb;
x = xa - xb;
vx = vax - vbx;
y = ya - yb;
vy = vay - vby;
r = ra + rb;
if (x * x + y * y == r * r)
{
printf ("%.3f %.3f %.3f %.3f %.3f\n" , 0.000 , xa , ya , xb , yb);
continue;
}
a = vx * vx + vy * vy;
b = 2 * (vx * x + y * vy);
c = x * x + y * y - r * r;
double temp = -1;
if (a == 0)
{
temp = (- c )/ b;
if (temp <= 0)
{
cout << "Impossible" << endl;
continue;
}
else
{
printf ("%.3f %.3f %.3f %.3f %.3f\n" , temp , xa + vax * temp , ya + vay * temp , xb + vbx * temp , yb + vby * temp);
}
}
dd = b * b - 4 * a * c ;
if (dd < 0)
{
cout << "Impossible" << endl;
continue;
}
sdd = sqrt (dd);
t1 = ((-b) + sdd) / (2 * a);
t2 = -(b + sdd) / (2 * a);
if (t1 > 0)
{
temp = t1;
if (t2 > 0 && temp > t2)
temp = t2;
}
else
temp = t2;
if (temp < 0)
{
cout << "Impossible" << endl;
continue;
}
printf ("%.3f %.3f %.3f %.3f %.3f\n" , temp , xa + vax * temp , ya + vay * temp , xb + vbx * temp , yb + vby * temp);
}
return 0;
}