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点距离最小。很明显的凸性函数,用三分法来解决。
时间限制: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点距离最小。很明显的凸性函数,用三分法来解决。
Calc函数即为求某点到P点的距离。
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
#include<math.h>
#define eps 1e-8
using namespace std;
struct point
{
double x,y,z;
};
point l,r,p;
point MID(point a,point b)
{
point k;
k.x=(a.x+b.x)*0.5;
k.y=(a.y+b.y)*0.5;
k.z=(a.z+b.z)*0.5;
return k;
}
double dist(point a,point b)
{
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));
}
int sgn(double a)
{
return (a>eps)-(a<-eps);
}
point work()
{
point mid,midmid;
while(sgn(dist(l,r))>0)
{
mid=MID(l,r);
midmid=MID(mid,r);
if(dist(mid,p)<dist(midmid,p))
r=midmid;
else
l=mid;
}
return r;
}
int main()
{
int t,i,j,k,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&p.x,&p.y,&p.z);
scanf("%lf%lf%lf",&l.x,&l.y,&l.z);
scanf("%lf%lf%lf",&r.x,&r.y,&r.z);
l=work();
printf("Case %d: %.2lf\n",cas++,dist(l,p));
}
return 0;
}