当前位置:   article > 正文

buaa 1033 Easy Problem(三分)(简单)

buaa 1033 easy problem
Easy Problem


时间限制:1000 ms  |  内存限制:65536 KB
描述
In this problem, you're to calculate the distance between a point P(xp, yp, zp) and a segment (x1, y1, z1) ? (x2, y2, z2), in a 3D space, i.e. the minimal distance from P to any point Q(xq, yq, zq) on the segment (a segment is part of a line).


输入
The first line contains a single integer T (1 ≤ T ≤ 1000), the number of test cases. Each test case is a single line containing 9 integers xp, yp, zp, x1, y1, z1, x2, y2, z2. These integers are all in [-1000,1000].


输出
For each test case, print the case number and the minimal distance, to two decimal places.


样例输入
3
0 0 0 0 1 0 1 1 0
1 0 0 1 0 1 1 1 0
-1 -1 -1 0 1 0 -1 0 -1
样例输出
Case 1: 1.00
Case 2: 0.71

Case 3: 1.00


题意:

为在一条线段上找到一点,与给定的P点距离最小。很明显的凸性函数,用三分法来解决。dist函数即为求某点到P点的距离。注意精度问题。

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #define eps 1e-8
  5. using namespace std;
  6. typedef struct node
  7. {
  8. double x,y,z;
  9. }node;
  10. node l,r,p;
  11. double dist(node a,node b)
  12. {
  13. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
  14. }
  15. int sgn(double a)
  16. {
  17. return (a>eps)-(a<-eps);
  18. }
  19. node getmid(node a,node b)
  20. {
  21. node mid;
  22. mid.x=(a.x+b.x)/2;
  23. mid.y=(a.y+b.y)/2;
  24. mid.z=(a.z+b.z)/2;
  25. return mid;
  26. }
  27. node search()
  28. {
  29. node mid,midmid;
  30. while(sgn(dist(l,r))>0)
  31. {
  32. mid=getmid(l,r);
  33. midmid=getmid(mid,r);
  34. if(dist(p,mid)<dist(p,midmid))
  35. r=midmid;
  36. else
  37. l=mid;
  38. }
  39. return r;
  40. }
  41. int main()
  42. {
  43. int t;node k;
  44. cin>>t;
  45. for(int i=1;i<=t;i++)
  46. {
  47. cin>>p.x>>p.y>>p.z;
  48. cin>>l.x>>l.y>>l.z;
  49. cin>>r.x>>r.y>>r.z;
  50. k=search();
  51. printf("Case %d: %.2lf\n",i,dist(k,p));
  52. }
  53. return 0;
  54. }




声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号