Codeforces Contest 1079 problem D 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=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.
Codeforces Contest 1079 problem D Barcelonian Distance —— 枚举

题意:

有一个城市,它的街道都是由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;
}

上一篇:vue-element-admin实战 | 第二篇: 最小改动接入后台实现根据权限动态加载菜单


下一篇:php-用户权限-触发有关用户角色的代码(WordPress)