题目链接:Barcelonian Distance

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

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

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

 #include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; double a,b,c,ans;
double x1,y1,x2,y2; double calx(double y){
return (-c-b*y)/a;
} double caly(double x){
return (-c-a*x)/b;
} double cal1(double x1,double x2){
double X1,X2,Y1,Y2;
double res=;
X1=x1;Y1=caly(x1);
res+=abs(Y1-y1);
X2=x2;Y2=caly(x2);
res+=abs(Y2-y2);
res+=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
return res;
} double cal2(double x1,double y2){
double X1,X2,Y1,Y2;
double res=;
X1=x1;Y1=caly(x1);
res+=abs(Y1-y1);
X2=calx(y2);Y2=y2;
res+=abs(X2-x2);
res+=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
return res;
} double cal3(double y1,double x2){
double X1,X2,Y1,Y2;
double res=;
X1=calx(y1);Y1=y1;
res+=abs(X1-x1);
X2=x2;Y2=caly(x2);
res+=abs(Y2-y2);
res+=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
return res;
} double cal4(double y1,double y2){
double X1,X2,Y1,Y2;
double res=;
X1=calx(y1);Y1=y1;
res+=abs(X1-x1);
X2=calx(y2);Y2=y2;
res+=abs(X2-x2);
res+=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
return res;
} int main(){
scanf("%lf%lf%lf",&a,&b,&c);
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
double ans=abs(x1-x2)+abs(y1-y2);
if(a==||b==){
printf("%.10f\n",abs(x1-x2)+abs(y1-y2));
return ;
}
ans=min(ans,cal1(x1,x2));
ans=min(ans,cal2(x1,y2));
ans=min(ans,cal3(y1,x2));
ans=min(ans,cal4(y1,y2));
printf("%.10f\n",ans);
return ;
}

Codeforces 1079D Barcelonian Distance(计算几何)的更多相关文章

  1. Codeforces I. Barcelonian Distance(暴力)

    题目描述: In this problem we consider a very simplified model of Barcelona city. Barcelona can be repres ...

  2. Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3) D. Barcelonian Distance 几何代数(简单)

    题意:给出一条直线 ax +by+c=0  给出两个整点 (x1,y1) (x2,y2) 只有在x,y坐标至少有一个整点的时 以及   给出的直线才有路径(也就是格子坐标图的线上) 问 两个整点所需要 ...

  3. Codeforces 161 D. Distance in Tree (树dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...

  4. Codeforces 528E Triangles 3000 - 计算几何

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定$n$的平面上的直线,保证没有三条直线共点,两条直线平行.问随机选出3条直线交成的三角形面积的期望. 显然$S=\frac{1}{2}ah ...

  5. Codeforces 608B. Hamming Distance Sum 模拟

    B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...

  6. codeforces 161 D. Distance in Tree(树形dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...

  7. Codeforces 161.D. Distance in Tree-树分治(点分治,不容斥版)-树上距离为K的点对数量-蜜汁TLE (VK Cup 2012 Round 1)

    D. Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  8. Codeforces Gym 100733A Shitália 计算几何

    ShitáliaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...

  9. Codeforces Gym 100286A. Aerodynamics 计算几何 求二维凸包面积

    Problem A. AerodynamicsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...

随机推荐

  1. form-data、x-www-form-urlencoded的区别

    form-data可以上传文件格式的,比如mp3.jpg这些:x-www-form-urlencoded不能选择格式文件,只能传key-value这种string格式的内容.

  2. docker 修改port 端口

    修改docker容器端口映射的方法 - wesleyflagon的专栏 - CSDN博客https://blog.csdn.net/wesleyflagon/article/details/78961 ...

  3. Memcached 集群架构与memcached-session-manager

    Memcached 集群架构方面的问题_知识库_博客园https://kb.cnblogs.com/page/69074/ memcached-session-manager配置 - 学习中间件调优管 ...

  4. jmeter的jtl日志转html报告常见报错笔记

    问题:生成的jmeter文件可以放任意位置 输入命令转换hmtl报告 PS D:\user\80003288\桌面\Ques> jmeter -g .\test1.jtl -e -o .\rep ...

  5. [转帖]Vim 编辑器底端 [noeol], [dos] 的含义

    Vim 编辑器底端 [noeol], [dos] 的含义 2012年11月28日 23:13:04 strongwangjiawei 阅读数:15484 https://blog.csdn.net/s ...

  6. Oracle 备份表数据

    --备份表数据 select * from t_owners; --创建备份表 create table t_owners_copy ( id number, name ), addressid nu ...

  7. 谷歌浏览器报错 Active resource loading counts reached to a per-frame

    Active resource loading counts reached to a per-frame limit while the tab is in background. Network ...

  8. 前K个高频元素

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...

  9. 如何在网页中用echarts图表插件做出静态呈现效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 一、kubeadm安装

    一.官网 https://kubernetes.io/zh/docs/setup/independent/install-kubeadm/ 阿里云 kubernetes yum 仓库镜像 安装kube ...