Goat in the Garden (ural 1348)
来源:互联网 发布:tcp 不同端口 并行 编辑:程序博客网 时间:2024/04/30 05:49
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;elsereturn 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-8using 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));elsed1=(d<d2? d:d2);d2=(d2>d? d2:d);}if(d2<=l)d2=0;elsed2=d2-l;if(d1<=l)d1=0;elsed1=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;}
- Goat in the Garden (ural 1348)
- ural 1084. Goat in the Garden
- ural 1348. Goat in the Garden 2
- ural 1084 Goat in the Garden
- ural 1084. Goat in the Garden math
- URAL 1348 Goat in the Garden 2(点到线段的距离)
- URAL 1348 Goat in the Garden 2计算几何(解题报告)
- URAL 1348 Goat in the Garden 2(点到线段的距离)
- Ural-1084. Goat in the Garden(计算几何)
- URAL 1348 Goat in the Garden 2(点到线段的距离)
- 【点到线段距离】URAL - 1348 Goat in the Garden 2
- Ural1084. Goat in the Garden
- timus 1084. Goat in the Garden URAL 解题报告 计算几何
- timus 1348. Goat in the Garden 2 URAL 点到线段的距离
- URAL Goat in the Garden(圆与矩形求交集面积)
- URAL 1348. Goat in the Garden 2[求点到线段的距离]
- URAL 1084 || Goat in the Garden(同中心矩形与圆的公共面积
- sgu 1348 Goat in the Garden 2【点到线段的距离】
- Java的集合类
- RFS的web自动化验收测试——第9讲 用户关键字User Keyword(1)
- 凤凰卫视8月21日《与幻想同行》,以下为书契实录
- Xcode4中制作和使用静态库
- POJ 1089 Intervals 区间覆盖+ 贪心
- Goat in the Garden (ural 1348)
- App Store申请和管理相关知识
- 对爱情向往着萌芽
- 《c和指针》函数指针
- iPhone锁屏代码
- cpp文件编译正确 c文件编译一堆错误
- iphone的IP地址源码
- ubuntu 硬盘安装指南(适用于11.10和12.04)
- Android屏幕禁止休眠和锁屏的方法