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,体积最大是多少. 析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选 ...
随机推荐
- css3网站响应式写法
css3响应式写法因为media不支持ie9以下的浏览器 所有要加个判断<pre> <!-- 全部通用的 --><link rel="stylesheet&qu ...
- 卓金武《MATLAB在数学建模中的应用》 第2版
内容介绍 本书的作者都具有实际的数学建模参赛经历和竞赛指导经验.书中内容完全是根据数学建模竞赛的需要而编排的,涵盖了绝大部分数学建模问题的matlab求解方法.本书内容分上下两篇.上篇介绍数学建模中常 ...
- ajax中如何使用全局变量?
在ajax中一般都是采取默认的异步请求,但是有时候参数是需要做到全局通用,这时候发起同步请求. 如下: $.ajax({ type:"post", url:"url路径& ...
- 递归实现全排列python
python递归实现"abcd"字符串全排列 1.保持a不动,动bcd 2.保持b不动,动cd 3.保持c不动,动d def pailie(head="",st ...
- ubuntu 安装和配置 GitLab
一.概述 GitLab 是一个基于 Web 的开源 Git 软件仓库管理器,用 Ruby 编写,包括 wiki,问题管理,代码审查,监控以及持续集成和部署.它使开发人员能够创建,审查和部署他们的项目. ...
- 20、Outer Apply 和 Cross Apply
1.場合 select...caseが複雑の時 2.運用方法 SELECT * FROM stu CROSS APPLY ( --like inner join * FROM score WHERE ...
- eclipse不提示问题
按照上面截图输入26个字母大小写,即可.
- Java ClassLoader 学习理解
/** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> * <p> ...
- 自己使用的jquery公用common.js
/*解决ie8中js数组没有indexOf方法*/ jQuery.extend({ exportResport : function(url, method, params){ var paramCo ...
- 2019 东软java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.东软等公司offer,岗位是Java后端开发,最终选择去了东软. 面试了很多家公司,感觉大部分公司考察的点都差不多 ...