Time limit:1000 ms Memory limit:65536 KB
Total Submit:1760 (464 users) Accepted Submit:367 (333 users)
Description
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).
Input
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].
Output
For each test case, print the case number and the minimal distance, to two decimal places.
Sample Input
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
Sample Output
Case 1: 1.00
Case 2: 0.71
Case 3: 1.00
Problem Source
The 32nd ACM-ICPC Beijing First Round Internet Contest
This is the request for one of the five test questions of ACM (Association of Computing Machinery, simply called ACM: 美国计算机协会) International Collegiate Programming Contest (ACM-ICPC or ICPC: ACM国际大学生程序设计竞赛) which I took part in last year (2007). And I answered this problem as followed:
#include<stdio.h>
#include<math.h>
int main()
{
int x0,y0,z0;
int x1,y1,z1;
int x2,y2,z2;
float a,b,c,d,p,s;
int i = 0, j = 0;
int n;
scanf("%d",&n);
float distant[100];
if (n > 0 && n < 1000)
{
for (i = 0; i < n; i++)
{
scanf("%d%d%d%d%d%d%d%d%d",&x0,&y0,&z0,&x1,&y1,&z1,&x2,&y2,&z2);
a = sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1) + (z0 - z1) * (z0 - z1));
b = sqrt((x0 - x2) * (x0 - x2) + (y0 - y2) * (y0 - y2) + (z0 - z2) * (z0 - z2));
c = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1));
p = (a + b + c ) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
d = (2 * s) / c;
if (a * a + c * c < b * b || b * b + c * c < a * a || abs(a - b) == c)
{
if (a > b)
distant[i] = b;
else
distant[i] = a;
}
else
{
distant[i] = d;
}
}
for (j = 0; j < n; j++)
{
printf("Case %d: % 5.2f /n",j + 1,distant[j]);
}
}
return 0;
}
But at last, I didn’t know why this part of code wasn’t been received by the ACM-ICPC server. I just want to know why. This method had made me thinking too long times. The time and memory used by this code when it running has not out of its limits, and the input parameters & the output results are all right too – as the same as the example has been given. I think there was no problem for that kind of algorithm.
Then I had finished two of the five questions between the period of the exam, and what made me feel glade is that I thought the way to solve this problem.
rel="File-List" href="file:///C:%5CDOCUME%7E1%5Csyn%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">My written English is not well, so if any where of this topic has some error in grammar of words, please give me a tip and I’ll improve my English writing.