码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 1079D Barcelonian Distance(计算几何)

时间:2018-11-19 21:35:52      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:arc   opened   hid   onclick   force   技术分享   turn   abs   none   

题目链接:Barcelonian Distance

题意:给定方格坐标,方格坐标上有两个点A,B和一条直线。规定:直线上沿直线走,否则沿方格走。求A到B的最短距离。

题解:通过直线到达的:A、B两点都有两种方式到直线上,最多4种情况,每种情况求出A、B点到直线的距离和直线上新的两点间距离,取4种情况中最优的。

不通过直线到达:$abs(x1-x2)+abs(y1-y2)$,最后与通过直线到达的最优情况比较,得到最优解。

技术分享图片
 1 #include <cmath>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 double a,b,c,ans;
 7 double x1,y1,x2,y2;
 8 
 9 double calx(double y){
10     return (-c-b*y)/a;
11 }
12 
13 double caly(double x){
14     return (-c-a*x)/b;
15 }
16 
17 double cal1(double x1,double x2){
18     double X1,X2,Y1,Y2;
19     double res=0;
20     X1=x1;Y1=caly(x1);
21     res+=abs(Y1-y1);
22     X2=x2;Y2=caly(x2);
23     res+=abs(Y2-y2);
24     res+=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
25     return res;
26 }
27 
28 double cal2(double x1,double y2){
29     double X1,X2,Y1,Y2;
30     double res=0;
31     X1=x1;Y1=caly(x1);
32     res+=abs(Y1-y1);
33     X2=calx(y2);Y2=y2;
34     res+=abs(X2-x2);
35     res+=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
36     return res;
37 }
38 
39 double cal3(double y1,double x2){
40     double X1,X2,Y1,Y2;
41     double res=0;
42     X1=calx(y1);Y1=y1;
43     res+=abs(X1-x1);
44     X2=x2;Y2=caly(x2);
45     res+=abs(Y2-y2);
46     res+=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
47     return res;
48 }
49 
50 
51 double cal4(double y1,double y2){
52     double X1,X2,Y1,Y2;
53     double res=0;
54     X1=calx(y1);Y1=y1;
55     res+=abs(X1-x1);
56     X2=calx(y2);Y2=y2;
57     res+=abs(X2-x2);
58     res+=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
59     return res;
60 }
61 
62 int main(){
63     scanf("%lf%lf%lf",&a,&b,&c);
64     scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
65     double ans=abs(x1-x2)+abs(y1-y2);
66     if(a==0||b==0){
67         printf("%.10f\n",abs(x1-x2)+abs(y1-y2));
68         return 0;
69     }
70     ans=min(ans,cal1(x1,x2));
71     ans=min(ans,cal2(x1,y2));
72     ans=min(ans,cal3(y1,x2));
73     ans=min(ans,cal4(y1,y2));
74     printf("%.10f\n",ans);
75     return 0;
76 }
View Code

 

Codeforces 1079D Barcelonian Distance(计算几何)

标签:arc   opened   hid   onclick   force   技术分享   turn   abs   none   

原文地址:https://www.cnblogs.com/ehanla/p/9985319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!