Codeforces 1079D Barcelonian Distance(计算几何)

Posted ehanla

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1079D Barcelonian Distance(计算几何)相关的知识,希望对你有一定的参考价值。

题目链接: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
",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
",ans);
75     return 0;
76 }
View Code

 

以上是关于Codeforces 1079D Barcelonian Distance(计算几何)的主要内容,如果未能解决你的问题,请参考以下文章

如何看codeforces做了多少题

codeforces上怎么看测试数据

codeforces比赛后怎么看题解和答案

codeforces是啥?

codeforces Codeforces 650A Watchmen

CodeForces - 504A && CodeForces - 624C && CodeForces - 2B