题目描述:

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(暴力)的更多相关文章

  1. Codeforces 1079D Barcelonian Distance(计算几何)

    题目链接:Barcelonian Distance 题意:给定方格坐标,方格坐标上有两个点A,B和一条直线.规定:直线上沿直线走,否则沿方格走.求A到B的最短距离. 题解:通过直线到达的:A.B两点都 ...

  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 724B Batch Sort(暴力-列交换一次每行交换一次)

    题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...

  4. codeforces 897A Scarborough Fair 暴力签到

    codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...

  5. Codeforces A. Playlist(暴力剪枝)

    题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. [codeforces 200 A Cinema]暴力,优化

    题意大致是这样的:有一个有n行.每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x.y从小到大排序然后取最小的.输出 ...

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

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

  8. Codeforces Gym 100531D Digits 暴力

    Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...

  9. CodeForces 589B Layer Cake (暴力)

    题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少. 析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选 ...

随机推荐

  1. php异常处理小总结

    2019年8月23日10:56:31 php很多开发不习惯使用异常处理,因为web开发,重在于快速开发,易用性,高性能,不强调程序健壮性 php的异常使用其实不是太完善,易用性也差点,当然这个对比其他 ...

  2. mysql:获取某个表的所有字段

    select COLUMN_NAME from information_schema.COLUMNS where table_name = '表名' and table_schema = '数据库名' ...

  3. ThreadLocal源代码3

    public class ThreadLocal1<T> { //当创建了一个 ThreadLocal 的实例后,它的散列值就已经确定了, //threadLocal实例的hashCode ...

  4. PHP7之Trait详解

    转自: https://www.jianshu.com/p/fc053b2d7fd1 php从以前到现在一直都是单继承的语言,无法同时从两个基类中继承属性和方法,为了解决这个问题,php出了Trait ...

  5. 【转帖】Linux命令行操作json神器jq

    Linux命令行操作json神器jq https://www.cnblogs.com/chenqionghe/p/11736942.html jq类似一个awk或grep一样的神器,可以方便地在命令行 ...

  6. 【转帖】Intel AMD 龙芯2019年12月份最新产品线

    Intel未来三代U集体曝光:14nm退回去了! https://news.cnblogs.com/n/651244/ 不过没搞懂 为啥中芯国际已经开始量产14nm了 龙芯为什么不用.. 3A4000 ...

  7. 工厂方法(FactoryMethod)模式

    之前说了简单工厂设计模式如果增加一个新的运算的时候需要:增加一个具体的实现类,工厂类中增加一个case分支.也就是说我们不但对扩展开发了,也对修改开放了,违背了开闭原则.当然如果工厂类采用反射的话不存 ...

  8. SSM整合学习 一

    一:新建maven项目 File--New Project选择maven项目下的maven-archetype-webapp,输入GroupId.Artifactld,选择maven信息,新建mave ...

  9. golang中生成读取二维码(skip2/go-qrcode和boombuler/barcode,tuotoo/qrcode)

     1 引言 在github上有好用golan二维码生成和读取库,两个生成二维码的qrcode库和一个读取qrcode库. skip2/go-qrcode生成二维码,github地址:https://g ...

  10. python入门基础 02

    目录 1.while 2.字符串格式化 3.运算符 4.编码初始 总结 1.while # while -- 关键字 (死循环) # # if 条件: # 结果 # # while 条件: # 循环体 ...