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

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

另一个博客上有个详细的描述: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 10
60 0 10
30 30 10
output
30.00000 0.00000

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

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

import math

def corner(x,y,o_x,o_y,r):
    #sin value
    dis=math.sqrt((x-o_x)**2 + (y-o_y)**2)
    return r/dis

def 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 result

def 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 = 0
while 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值