NC22428. Interstellar … Fantasy
描述
输入描述
The first line contains an integer, the number of test cases. Then T test cases follow.
Each test case contains two lines.
The first line contains four integers, the positions in each dimension of the center of the sphere o and its radius.
The second line contains six integers, the positions in each dimension of the starting point s and the terminal point t, respectively. Notice that s and t may be at the same position.
It is guaranteed that neither s nor t is strictly inside the obstacle, and each value of a position or a radius in the input belongs to [1, 1000].
输出描述
Output one number, the minimum distance from s to t without entering the sphere obstacle. Your answer is considered correct if the absolute or relative error doesn't exceed.
示例1
输入:
2 2 1 1 1 1 1 1 3 1 1 2 1 1 1 1 2 2 3 1 1
输出:
3.14159265 2.64517298
C++ 解法, 执行用时: 5ms, 内存消耗: 420K, 提交时间: 2022-07-07 12:17:55
#include<cstdio> #include<cmath> int n,m,a,b,c,d,x,y,z,t; double sx,sy,sz,tx,ty,tz,r,r1,r2,r3,th1,th2,th,ans; int main() { scanf("%d",&t); for(int i=1;i<=t;i++) { scanf("%d%d%d%d",&a,&b,&c,&d); scanf("%d%d%d",&x,&y,&z); sx=x-a; sy=y-b; sz=z-c; scanf("%d%d%d",&x,&y,&z); tx=x-a; ty=y-b; tz=z-c; r=d; r1=sqrt(sx*sx+sy*sy+sz*sz); r2=sqrt(tx*tx+ty*ty+tz*tz); r3=sqrt((tx-sx)*(tx-sx)+(ty-sy)*(ty-sy)+(tz-sz)*(tz-sz)); th=(r1*r1+r2*r2-r3*r3)/(2*r1*r2); th=acos(th); th1=acos(r/r1); th2=acos(r/r2); if((th2+th1)>=th)ans=r3; else ans=(th-th2-th1)*r+sqrt(r1*r1-r*r)+sqrt(r2*r2-r*r); printf("%.8lf\n",ans); } }