SOJ2658.LineDist

来源:互联网 发布:户型优化设计大赛 编辑:程序博客网 时间:2024/06/08 00:20

http://soj.me/2658

 

2658. LineDist
    
    
Time Limit: 1sec    Memory Limit:256MB
Description
In n-dimension space, a line can be determined by two distinct points. Your task is to calculate the distance between the two lines. Distance between two lines is defined as the minimal distance of two points in the two lines respectively. Distance of two points A(x1,x2,…xn) and B(y1,y2,…,yn) is defined as sqrt( (x1-y1)^2 + (x2-y2)^2 + … + (xn-yn)^2 ).
Input
There are several test cases. For each case, there are five lines, the first line contains the integer number n, and then each of the next four lines contains n integer numbers, these data are for four points A, B, C and D. A and B are distinct and determines line AB, C and D are distinct and determines line CD, you should calculate distance between AB and CD.
Constraints: 2 <= n <= 3, other input numbers will be between -1000 and 1000.
Input is ended with n = 0.
Output
Output each result in a single line, with two digit precision after the decimal point.
Sample Input
2
0 0
0 1
1 0
1 1
2
2 0
0 2
-1 0
0 1
3
0 0 0
0 0 1
1 1 0
1 1 1
3
0 0 0
0 0 1
0 1 0
1 1 0
0
Sample Output
1.00
0.00
1.41
1.00

题意:
给两条2维或者3维的直线求最短距离
大致思路:
对与一点,一条直线上的点与这个固定点的距离是个单峰函数;若这个点在某条线上,那么这个点与另外一条直线的最短距离也是个单峰函数,所以
用两重三分。
由于坐标在[-1000, 1000],所以最短距离所在点的绝对值会在[0, 10003]内。
程序:
 

 

原创粉丝点击