ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Contest 1079 problem D Barcelonian Distance —— 枚举

2019-01-27 14:04:33  阅读:377  来源: 互联网

标签:Barcelonian x1 minn fabs Contest 1079 x2 y1 y2


In this problem we consider a very simplified model of Barcelona city.

Barcelona can be represented as a plane with streets of kind x=c and y=c for every integer c (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) for which ax+by+c=0.

One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.

Input
The first line contains three integers a, b and c (−109≤a,b,c≤109, at least one of a and b is not zero) representing the Diagonal Avenue.

The next line contains four integers x1, y1, x2 and y2 (−109≤x1,y1,x2,y2≤109) denoting the points A=(x1,y1) and B=(x2,y2).

Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10−6.

Formally, let your answer be a, and the jury’s answer be b. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−6.

Examples
inputCopy
1 1 -3
0 3 3 0
outputCopy
4.2426406871
inputCopy
3 1 -9
0 3 3 -1
outputCopy
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
在这里插入图片描述

题意:

有一个城市,它的街道都是由x=c,y=c(c=1,2,3…)组成的,有一条街道以ax+by+c=0的形式出现,问你从A到B走街道最短距离是多少。

题解:

5种情况,直接曼哈顿距离,A从x轴到特殊街道,从y轴到特殊街道,B也一样,然后算一算即可。

#include<bits/stdc++.h>
using namespace std;
#define exp 1e-8
double dis(double x1,double y1,double x2,double y2)
{
    return sqrt(fabs(x1-x2)*fabs(x1-x2)+fabs(y1-y2)*fabs(y1-y2));
}
double x[5],y[5];
int main()
{
    double a,b,c;
    double x1,y1,x2,y2;
    scanf("%lf%lf%lf%lf%lf%lf%lf",&a,&b,&c,&x1,&y1,&x2,&y2);
    double minn=fabs(x1-x2)+fabs(y1-y2);
    x[1]=x1,y[1]=(-a*x1-c)/b;
    x[2]=(-b*y1-c)/a,y[2]=y1;
    x[3]=x2,y[3]=(-a*x2-c)/b;
    x[4]=(-b*y2-c)/a,y[4]=y2;
    minn=min(minn,fabs(y[1]-y1)+fabs(y2-y[3])+dis(x[1],y[1],x[3],y[3]));
    minn=min(minn,fabs(y[1]-y1)+fabs(x2-x[4])+dis(x[1],y[1],x[4],y[4]));
    minn=min(minn,fabs(x1-x[2])+fabs(y2-y[3])+dis(x[2],y[2],x[3],y[3]));
    minn=min(minn,fabs(x1-x[2])+fabs(x2-x[4])+dis(x[2],y[2],x[4],y[4]));
    printf("%.10f\n",minn);
    return 0;
}

标签:Barcelonian,x1,minn,fabs,Contest,1079,x2,y1,y2
来源: https://blog.csdn.net/tianyizhicheng/article/details/86665643

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有