hdoj4617Weapon【空间直线间的距离】

来源:互联网 发布:mac内存清理 编辑:程序博客网 时间:2024/05/22 01:50



Weapon

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 412    Accepted Submission(s): 330


Problem Description
  Doctor D. are researching for a horrific weapon. The muzzle of the weapon is a circle. When it fires, rays form a cylinder that runs through the circle verticality in both side. If one cylinder of rays touch another, there will be an horrific explosion. Originally, all circles can rotate easily. But for some unknown reasons they can not rotate any more. If these weapon can also make an explosion, then Doctor D. is lucky that he can also test the power of the weapon. If not, he would try to make an explosion by other means. One way is to find a medium to connect two cylinder. But he need to know the minimum length of medium he will prepare. When the medium connect the surface of the two cylinder, it may make an explosion.
 

Input
  The first line contains an integer T, indicating the number of testcases. For each testcase, the first line contains one integer N(1 < N < 30), the number of weapons. Each of the next 3N lines&nbsp; contains three float numbers. Every 3 lines represent one weapon. The first line represents the coordinates of center of the circle, and the second line and the third line represent two points in the circle which surrounds the center. It is supposed that these three points are not in one straight line. All float numbers are between -1000000 to 1000000.
 

Output
  For each testcase, if there are two cylinder can touch each other, then output 'Lucky', otherwise output then minimum distance of any two cylinders, rounded to two decimals, where distance of two cylinders is the minimum distance of any two point in the surface of two cylinders.
 

Sample Input
330 0 01 0 00 0 15 2 25 3 25 2 310 22 -211 22 -111 22 -330 0 01 0 1.51 0 -1.5112 115 109114 112 110109 114 111-110 -121 -130-115 -129 -140-104 -114 -119.80196130 0 01 0 1.51 0 -1.5112 115 109114 112 110109 114 111-110 -121 -130-120 -137 -150-98 -107 -109.603922
 

Sample Output
Lucky2.32Lucky
 


题意:给定无限高的圆柱的截面的一个圆心和圆上两点判断两个圆柱是否相交:

解题思路:求出园的法向量判断两个比较两个圆法向量的距离即可

#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#define eps 1e-8#define inf 0x3f3f3f3fusing namespace std;struct point{double x,y,z;}vec[35];struct circle{double r;point a,b,c;}A[35];double MIN(double a,double b){return a<b?a:b;}void cp(point a,point b,point &vecc){    double i=a.y*b.z-a.z*b.y;      double j=a.z*b.x-a.x*b.z;      double k=a.x*b.y-a.y*b.x;     vecc.x=i;vecc.y=j;vecc.z=k;}   int main(){int t,i,j,k,n;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<n;++i){scanf("%lf%lf%lf",&A[i].a.x,&A[i].a.y,&A[i].a.z);scanf("%lf%lf%lf",&A[i].b.x,&A[i].b.y,&A[i].b.z);scanf("%lf%lf%lf",&A[i].c.x,&A[i].c.y,&A[i].c.z);A[i].r=sqrt((A[i].b.x-A[i].a.x)*(A[i].b.x-A[i].a.x)+(A[i].b.y-A[i].a.y)*(A[i].b.y-A[i].a.y)+(A[i].b.z-A[i].a.z)*(A[i].b.z-A[i].a.z));point vec1,vec2;vec1.x=A[i].b.x-A[i].a.x;vec1.y=A[i].b.y-A[i].a.y;vec1.z=A[i].b.z-A[i].a.z;vec2.x=A[i].c.x-A[i].a.x;vec2.y=A[i].c.y-A[i].a.y;vec2.z=A[i].c.z-A[i].a.z;cp(vec1,vec2,vec[i]);//叉积求法向量 }double ans=inf;bool flag=false;for(i=0;i<n;++i){for(j=i+1;j<n;++j){point vec1;cp(vec[i],vec[j],vec1);if(vec1.x==0&&vec1.y==0&&vec1.z==0){vec1.x=A[j].a.x-A[i].a.x;vec1.y=A[j].a.y-A[i].a.y;vec1.z=A[j].a.z-A[i].a.z;point vec2;cp(vec[i],vec1,vec2);double temp=sqrt((vec2.x*vec2.x+vec2.y*vec2.y+vec2.z*vec2.z)/(vec[i].x*vec[i].x+vec[i].y*vec[i].y+vec[i].z*vec[i].z));//当平行时两条直线的距离 if(temp<=A[i].r+A[j].r){flag=true;printf("Lucky\n");break;}else ans=MIN(ans,temp-A[i].r-A[j].r);}else {double temp=fabs(((A[j].a.x-A[i].a.x)*vec1.x+(A[j].a.y-A[i].a.y)*vec1.y+(A[j].a.z-A[i].a.z)*vec1.z)/sqrt(vec1.x*vec1.x+vec1.y*vec1.y+vec1.z*vec1.z));//不平行时两条空间直线的距离  if(temp<=A[i].r+A[j].r){flag=true;printf("Lucky\n");break;}else ans=MIN(ans,temp-A[i].r-A[j].r);}}if(flag)break;}if(!flag)printf("%.2lf\n",ans);}return 0;}
0 0
原创粉丝点击