CF1079D Barcelonian Distance
思路:
模拟。
实现:
#include <bits/stdc++.h>
using namespace std;
const long long INF = ;
double dis(double x1, double y1, double x2, double y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main()
{
int a, b, c;
double x1, x2, y1, y2;
while (cin >> a >> b >> c)
{
cin >> x1 >> y1 >> x2 >> y2;
if (a == || b == )
{
printf("%.10f\n", fabs(x1 - x2) + fabs(y1 - y2));
continue;
}
double n1 = x1, m1 = y2; // y1--y2
double ty = (-a * x1 - c) / b;
double tx = (-b * y2 - c) / a;
double ans = fabs(x1 - x2) + fabs(y1 - y2);
if (ty >= min(y1, y2) && ty <= max(y1, y2) && tx >= min(x1, x2) && tx <= max(x1, x2))
{
double tmp = fabs(y1 - ty) + fabs(x2 - tx) + dis(x1, ty, tx, y2);
ans = min(ans, tmp);
}
double ty2 = (-a * x2 - c) / b;
if (ty >= min(y1, y2) && ty <= max(y1, y2) && ty2 >= min(y1, y2) && ty2 <= max(y1, y2))
{
double tmp = fabs(y1 - ty) + fabs(ty2 - y2) + dis(x1, ty, x2, ty2);
ans = min(ans, tmp);
}
double n2 = x2, m2 = y1; // x1--x2
tx = (-b * y1 - c) / a;
ty = (-a * x2 - c) / b;
if (ty >= min(y1, y2) && ty <= max(y1, y2) && tx >= min(x1, x2) && tx <= max(x1, x2))
{
double tmp = fabs(x1 - tx) + fabs(y2 - ty) + dis(tx, y1, x2, ty);
ans = min(ans, tmp);
}
double tx2 = (-b * y2 - c) / a;
if (tx >= min(x1, x2) && tx <= max(x1, x2) && tx2 >= min(x1, x2) && tx2 <= max(x1, x2))
{
double tmp = fabs(x1 - tx) + fabs(tx2 - x2) + dis(tx, y1, tx2, y2);
ans = min(ans, tmp);
}
printf("%.10f\n", ans);
}
return ;
}
CF1079D Barcelonian Distance的更多相关文章
- Codeforces 1079D Barcelonian Distance(计算几何)
题目链接:Barcelonian Distance 题意:给定方格坐标,方格坐标上有两个点A,B和一条直线.规定:直线上沿直线走,否则沿方格走.求A到B的最短距离. 题解:通过直线到达的:A.B两点都 ...
- Codeforces I. Barcelonian Distance(暴力)
题目描述: In this problem we consider a very simplified model of Barcelona city. Barcelona can be repres ...
- 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坐标至少有一个整点的时 以及 给出的直线才有路径(也就是格子坐标图的线上) 问 两个整点所需要 ...
- Codeforces 1032 - A/B/C/D/E - (Undone)
链接:http://codeforces.com/contest/1032/ 是真的真的真的忍不住想吐槽这题意是真的真的真的读不懂…… A - Kitchen Utensils - [简单数学题] 题 ...
- Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3) Solution
A. Kitchen Utensils Water. #include <bits/stdc++.h> using namespace std; #define N 110 int n, ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
随机推荐
- oracle 左右链接
数据表的连接有: 1.内连接(自然连接): inner只有两个表相匹配的行才能在结果集中出现 2.外连接: 包括 (1)左外连接(左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接 ...
- [HNOI 2010] 弹飞绵羊
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2002 [算法] LCT动态维护森林连通性 时间复杂度 : O(NlogN ^ 2) ...
- 《Kubernetes权威指南第2版》学习(二)一个简单的例子
1: 安装VirtualBox, 并下载CentOS-7-x86_64-DVD-1708.iso, 安装centOS7,具体过程可以百度. 2:开启centOS的SSH, 步骤如下: (1) yum ...
- 51nod1228
伯努利数 这个是答案 其中的b是伯努利数,可以n^2预处理 伯努利数n^2递推 #include<bits/stdc++.h> using namespace std; typedef l ...
- springmvc源码分析系列-请求处理流程
接上一篇-springmvc源码分析开头片 上一节主要说了一下springmvc与struts2的作为MVC中的C(controller)控制层的一些区别及两者在作为控制层方面的一些优缺点.今天就结合 ...
- 3.2-3.3 Hive中常见的数据压缩
一.数据压缩 1. 数据压缩 数据量小 *本地磁盘,IO *减少网络IO Hadoop作业通常是IO绑定的; 压缩减少了跨网络传输的数据的大小; 通过简单地启用压缩,可以提高总体作业性能; 要压缩的数 ...
- Redis缓存雪崩、缓存穿透、缓存击穿、缓存降级、缓存预热、缓存更新
Redis缓存能够有效地加速应用的读写速度,就DB来说,Redis成绩已经很惊人了,且不说memcachedb和Tokyo Cabinet之流,就说原版的memcached,速度似乎也只能达到这个级别 ...
- 如何设置Xcode模拟器地图的当前位置
使用模拟器上的地图的话,需要设置当前位置,开启定位后,才能定位准确. 一.选中模拟器:Debug - Location - Custom Location 弹出的纬经度坐标.纬经度.纬经度坐标,默认显 ...
- PhpStorm插件之Api Debugger
安装插件 File->Setting->Pluugins 搜索 Api Debugger 如何使用 安装完插件后,RESTART IDE,在编辑器右侧 即可找到最新安装的 Api D ...
- 给 UILabel 中的文字增加 line-through / Strikethrough (删除线)样式
iOS 6 中苹果引入了 NSStrikethroughStyleAttributeName 属性,用于设置 NSAttributedString 的删除线样式,用法如下: let attribute ...