Codeforces I. Barcelonian Distance(暴力)
题目描述:
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x=cx=c and y=cy=c for every integer cc (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x,y)(x,y) for which ax+by+c=0ax+by+c=0.
One can walk along streets, including the avenue. You are given two integer points AAand BB somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to BB from AA.
Input
The first line contains three integers aa, bb and cc (−109≤a,b,c≤109−109≤a,b,c≤109, at least one of aa and bb is not zero) representing the Diagonal Avenue.
The next line contains four integers x1x1, y1y1, x2x2 and y2y2 (−109≤x1,y1,x2,y2≤109−109≤x1,y1,x2,y2≤109) denoting the points A=(x1,y1)A=(x1,y1) and B=(x2,y2)B=(x2,y2).
Output
Find the minimum possible travel distance between AA and BB. Your answer is considered correct if its absolute or relative error does not exceed 10−610−6.
Formally, let your answer be aa, and the jury's answer be bb. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−6|a−b|max(1,|b|)≤10−6.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
思路:
刚开始,凭感觉做的是通过画图,像是从A点出发直着向上/向下到达直线,从A点出发直着向右/向左到达直线,与从B出发直着向上/向下到达直线,从B出发直着向右/向左到达直线,共四种组合的距离,加上曼哈顿距离,求最小距离
然后,想的是从A点开始,到与直线相交,的每一种情况下,B也同样处理得到距离的最小值,说的不太清楚,如图
对每个A来说,算出每个B路径下的总的距离,取最小。
这里面有几个路径在程序中没包括,就是上图的紫色和黄色,组合在一起的情况,不过这样算会最终超时
小心的是数据用long long,不然计算中会溢出,还有输出格式的设置,想要设置成浮点数(不用科学计数法),设置精度等等
知识点:暴力
代码:
(忽略dd函数,那是会查实的,程序实际调用的是dd1函数)
#include <iostream>
#include <cmath>
#include <iomanip>
#include <climits>
using namespace std;
long long a,b,c;
long long x1,y1;
long long x2,y2;
double dd1(long long x1,long long y1,long long x2,long long y2)
{
double dist1 = ;
double yy1 = (-a*x1-c)/(double)b;
dist1 += abs(y1-yy1);
double yy2 = (-a*x2-c)/(double)b;
dist1 += abs(y2-yy2);
dist1 += sqrt((x1-x2)*(x1-x2)+(yy1-yy2)*(yy1-yy2));
double dist2 = ;
double xx2 = (-b*y2-c)/(double)a;
dist2 += abs(x2-xx2);
dist2 += abs(y1-yy1);
dist2 += sqrt((x1-xx2)*(x1-xx2)+(yy1-y2)*(yy1-y2));
double dist3 = ;
double xx1 = (-b*y1-c)/(double)a;
dist3 += abs(xx1-x1);
dist3 += abs(x2-xx2);
dist3 += sqrt((xx1-xx2)*(xx1-xx2)+(y1-y2)*(y1-y2));
double dist4 = ;
dist4 += abs(xx1-x1);
dist4 += abs(y2-yy2);
dist4 += sqrt((xx1-x2)*(xx1-x2)+(y1-yy2)*(y1-yy2));
double dist = dist1;
dist = min(dist,dist2);
dist = min(dist,dist3);
dist = min(dist,dist4);
return dist;
}
double dd(int x1,int x2,int x3,int x4)
{
double mdist = INT_MAX;
double xx1 = (-b*y1-c)/(double)a;
double xx2 = (-b*y2-c)/(double)a;
int length = abs(x1-(int)xx1);
for(int i = ;i<=length;i++)
{
double dist = ;
int x;
if(a*x1+b*x2+c<)
{
x = x1+i;
}
else
{
x = x1-i;
}
dist += i;
double yy1 = (-a*x-c)/(double)b;
dist += abs(y1-yy1);
int length2 = abs(x2-(int)xx2);
for(int j = ;j<=length2;j++)
{
if(a*x2+b*x2+c<)
{
x = x2+j;
}
else
{
x = x2-j;
}
dist += j;
double yy2 = (-a*x-c)/(double)b;
dist += abs(y2-yy2);
dist += sqrt((x2-x1)*(x2-x1)+(yy1-yy2)*(yy1-yy2));
if(dist<mdist)
{
mdist = dist;
}
} }
double dist = dd1(x1,x2,y1,y2);
if(dist<mdist)
{
mdist = dist;
}
return mdist;
}
int main()
{
cin >> a >> b >> c;
cin >> x1 >> y1 >> x2 >> y2;
double dist = abs(x1-x2)+abs(y1-y2);
if(a!=&&b!=)
{
double ans = dd1(x1,y1,x2,y2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout << setprecision() << min(dist,ans) << endl;
}
else
{
cout.setf(ios_base::fixed,ios_base::floatfield);
cout << dist << endl;
}
return ;
}
Codeforces I. Barcelonian Distance(暴力)的更多相关文章
- Codeforces 1079D Barcelonian Distance(计算几何)
题目链接:Barcelonian Distance 题意:给定方格坐标,方格坐标上有两个点A,B和一条直线.规定:直线上沿直线走,否则沿方格走.求A到B的最短距离. 题解:通过直线到达的:A.B两点都 ...
- 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 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- codeforces 897A Scarborough Fair 暴力签到
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...
- Codeforces A. Playlist(暴力剪枝)
题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- [codeforces 200 A Cinema]暴力,优化
题意大致是这样的:有一个有n行.每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x.y从小到大排序然后取最小的.输出 ...
- Codeforces 161 D. Distance in Tree (树dp)
题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...
- Codeforces Gym 100531D Digits 暴力
Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...
- CodeForces 589B Layer Cake (暴力)
题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少. 析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选 ...
随机推荐
- net core 环境部署的坑
1.supervisor “no such file” error. 检查指令是否正确,路径.dotnet环境是否正常 2.Couldn‘t find a valid ICU package inst ...
- js 强制换行及 单行文字溢出时出现省略号
/*强制换行*/.f-break {word-break:break-all; /*支持IE,chrome,FF不支持*/ word-wrap:break-word;/*支持IE,chrome,FF* ...
- 使用Android SDK卸载厂家程序
ADB下载: 官网翻墙比较慢,这里推荐使用国内网站:https://www.androiddevtools.cn/ 下载 SDK Tools 和 SDK Platform-Tools: 两者分别 ...
- 第1课,python输出,输入,变量,运算
课程内容: 为什么要学习python 如何学python 实践体验 图片来源网络分享 为什么要学python: 简单 (设计如此) 强大(因为开源,有库) 如何学习python: 变量 --> ...
- 【数据结构】12.java源码关于ConcurrentHashMap
目录 1.ConcurrentMap的内部结构 2.ConcurrentMap构造函数 3.元素新增策略4.元素删除5.元素修改和查找6.特殊操作7.扩容8.总结 1.ConcurrentMap内部结 ...
- Python爬虫之旅(一):小白也能懂的爬虫入门
Python爬虫之旅(一):小白也能懂的爬虫入门 爬虫是什么 爬虫就是按照一定的规则,去抓取网页中的信息.爬虫流程大致分为以下几步: 向目标网页发送请求 获取请求的响应内容 按照一定的规则解析返回 ...
- LOJ2461 完美的队列 分块
传送门 如果对于每一个操作\(i\)找到这个操作中所有的数都被pop掉的时间\(ed_i\),那么剩下就直接差分覆盖一下就可以了. 那么考虑如何求出\(ed_i\).发现似乎并没有什么数据结构能够维护 ...
- 处女篇:自用C#后端SqlHelper.cs类
自用SqlHelper.cs类,此类来自软谋教育徐老师课程SqlHelper.cs! using System; using System.Collections; using System.Coll ...
- 转:Windows下交换CapsLock和左ctrl
Windows下交换CapsLock和左ctrlHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout中添加Scanco ...
- java之基本技术点总结博客
泛型的理解 聊一聊-JAVA 泛型中的通配符 T,E,K,V,? 类,接口的继承和实现的规则 类与类之间只能继承,并且是单继承,可以多级继承 类与接口之间可以实现,一个类可以实现多个接口 接口和接口之 ...