Goat in the Garden (ural 1348)

http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=11732#problem/E 

E - Goat in the Garden 2

Time Limit:1000MS     Memory Limit:16384KB     64bit IO Format:%I64d & %I64uSubmit Status Practice URAL 1348

Description

A goat is tied to a peg (in a point C) in a garden with a strong rope of the length L (i.e. a goat may eat a grass that is not farther than Lmeters from the peg). There is a bed of pineapples that he loves very much. The bed is a line segment with the ends A and B.

Humph… We wonder, how much the goat is to stretch the roap in order to reach at least one pine apple? And all the pineapples?

Input

There are points’ A, B and C coordinates and a length of the rope L in the input. All the numbers are integer, L ≥ 0, all the coordinates don’t exceed 10000 by the absolute value. The numbers are separated with spaces or line feeds.

Output

The first line should contain the minimal length that the goat is to elongate the rope in order to reach the pineapples bed. The second line should contain the minimal length that the goat is to elongate the rope in order to eat all the pineapples from the bed. All the numbers are to be outputted within two digits after a decimal point.

Sample Input

input

output

8 -6 8 6

0 0 7

3.00

思路:求点到线段的最短和最长距离;:

方法两种,一直接有几何公式代入

有几个公式的应用:直线方程:Ax+By+C=0;点到直线的距离:d=|Ax+By+C|/(sqrt(A*A+B*B));

二·三分法查找

这里给出我自己写的几何法,及三分法的核心代码

#include<stdio.h>

#include<math.h>

#include<iostream>

using namespace std;

const double eps=1e-8;

int is_online(double x1,double y1,double x2,double y2,double x,double y)//判断(x,y)是否在线段上 

{

if(((x-x1)*(x-x2)+(y-y1)*(y-y2))<=eps)//(注意精度问题) 

return 1;

else

return 0;

}

int main()

{

    int i,j;

    double A,B,C1,C2,d1,d2,l,d;

    double a1,a2,b1,b2,c1,c2,x,y;

    while(scanf("%lf%lf%lf%lf",&a1,&a2,&b1,&b2)!=EOF)

    {scanf("%lf%lf%lf",&c1,&c2,&l);

    A=b2-a2;

    B=a1-b1;

    C1=a1*(a2-b2)-a2*(a1-b1);//Ax+By=C1表示直线L1方程 

    C2=A*c2-B*c1;//Bx-Ay+C2=0表示过圆心且垂直于直线L1的方程 

    //printf("%.2lf %.2lf %.2lf\n",A,B,C);

    if(A==0&&B==0)

    d1=d2=sqrt((a1-c1)*(a1-c1)+(a2-c2)*(a2-c2));//两点重合的情况 

    else

    { d=sqrt((a1-c1)*(a1-c1)+(a2-c2)*(a2-c2));

      d2=sqrt((b1-c1)*(b1-c1)+(b2-c2)*(b2-c2));

    y=(A*C2-B*C1)/(A*A+B*B);//(x,y)表示L1于L2的交点 

    x=(A*C1+B*C2)/(A*A+B*B)*(-1.0);

    if(is_online(a1,a2,b1,b2,x,y))

    d1=fabs(A*c1+B*c2+C1)/(sqrt(A*A+B*B));//点到直线的共识 

    else

    d1=(d<d2? d:d2);

    d2=(d2>d? d2:d);

    }

    //printf("%.2lf %.2lf",x,y);

    if(d2<=l)

    d2=0;

    else

    d2=d2-l; 

    if(d1<=l)

    d1=0;

    else

    d1=d1-l;

    printf("%.2lf\n%.2lf\n",d1,d2);

    }

    return 0;

}

#include<stdio.h>

#include<math.h>

#include<iostream>

#define eps 1e-8

using namespace std;

int is_online(double x1,double y1,double x2,double y2,double x,double y)//判断(x,y)是否在线段上 

{

double temp1=(x2-x)*(y1-y)-(y2-y)*(x1-x);//点在直线上

double temp2=(x-x1)*(x-x2);//点在线段中

double temp3=(y-y1)*(y-y2);

if(temp1<=eps&&(temp2<=eps&&temp3<=eps))

return 1;

return 0;

}

int main()

{

    int i,j;

    double A,B,C1,C2,d1,d2,l,d;

    double a1,a2,b1,b2,c1,c2,x,y;

    while(scanf("%lf%lf%lf%lf",&a1,&a2,&b1,&b2)!=EOF)

    {

scanf("%lf%lf%lf",&c1,&c2,&l);

A=b2-a2;

B=a1-b1;

C1=a1*(a2-b2)-a2*(a1-b1);//A B C1为线段方程的系数

C2=A*c2-B*c1;

if(A==0&&B==0)

d1=d2=sqrt((a1-c1)*(a1-c1)+(a2-c2)*(a2-c2));

else

{

d=sqrt((a1-c1)*(a1-c1)+(a2-c2)*(a2-c2));

d2=sqrt((b1-c1)*(b1-c1)+(b2-c2)*(b2-c2));

y=(A*C2-B*C1)/(A*A+B*B);

x=(A*C1+B*C2)/(A*A+B*B)*(-1.0);

if(is_online(a1,a2,b1,b2,x,y))

d1=fabs(A*c1+B*c2+C1)/(sqrt(A*A+B*B));

else

d1=(d<d2? d:d2);

d2=(d2>d? d2:d);

}

if(d2<=l)

d2=0;

else

d2=d2-l;

if(d1<=l)

d1=0;

else

d1=d1-l;

printf("%.2lf\n%.2lf\n",d1+eps,d2+eps);

}

    return 0;

}


三分核心代码

double three()

{

double d1,d2;

Node l,r,mid,midmid;

l.x=l1.x,l.y=l1.y;

r.x=l2.x,r.y=l2.y;

d1=1,d2=0;

while(fabs(d1-d2)>esp){

mid.x=(l.x+r.x)/2;

mid.y=(l.y+r.y)/2;

midmid.x=(mid.x+r.x)/2;

midmid.y=(mid.y+r.y)/2;

d1=dis(mid,o);

d2=dis(midmid,o);

if(d1<d2)

r=midmid;

else l=mid;

}

return d1;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值