算法题 求三个圆的等视角点

来源:互联网 发布:利用淘宝规则赚钱 编辑:程序博客网 时间:2024/06/04 19:15

京东算法岗考了的一道算法题

另一个博客上有个详细的描述:http://blog.csdn.net/snowy_smile/article/details/50131317

内容:

C. Commentator problem
time limit per test
 1 second
memory limit per test
 64 megabytes
input
 standard input
output
 standard output

The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered.

Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other.

Input

The input data consists of three lines, each of them describes the position of one stadium. The lines have the format x,  y,  r, where (x, y) are the coordinates of the stadium's center ( -  103 ≤ x,  y ≤ 103), and r (1 ≤ r  ≤ 103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line.

Output

Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank.

Sample test(s)
input
0 0 1060 0 1030 30 10
output
30.00000 0.00000

那篇文章中给出了C代码。

自己用的python写的,放上来python代码。

import mathdef corner(x,y,o_x,o_y,r):    #sin value    dis=math.sqrt((x-o_x)**2 + (y-o_y)**2)    return r/disdef cost_func(ang):    #cost function, ang is a three dimension vector    result=0    for i in range(3):        result+=(ang[i]-ang[(i+1)%3])**2    return resultdef cal_cost(x,y,three_point):    ang=[]    for i in range(3):        cor=corner(x,y,three_point[i][0],three_point[i][1],three_point[i][2])        ang.append(cor)    return cost_func(ang)flag = 0while 1:    if flag==1:        break    #save data(x,y,r)    input_list=[]    for _ in range(3):        s = raw_input().split()        if s==[]:            flag=1            break        x,y,r = int(s[0]),int(s[1]),int(s[2])        input_list.append((x,y,r))    #init    center_x = sum([x[0] for x in input_list])/3    center_y = sum([x[1] for x in input_list])/3    evr = cal_cost(center_x,center_y,input_list)    #move vector    dx = (-1,0,1,0)    dy = (0,-1,0,1)    step = 1    for _ in xrange(10**6):        for i in range(4):            x = center_x + step * dx[i]            y = center_y + step * dy[i]            new_cost=cal_cost(x,y,input_list)            if new_cost< evr:                center_x=x                center_y=y                evr=new_cost                break        else:            step=step/2        if step<= 10**(-6):            break    print center_x,center_y


0 0
原创粉丝点击