K - Keeping the Dogs Apart GYM-101550(计算几何)

2024-04-16 00:58

本文主要是介绍K - Keeping the Dogs Apart GYM-101550(计算几何),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

参考:https://blog.csdn.net/xbb224007/article/details/79846472

题意:
给出两只狗的路线,且其速度相同,求其运动时候的最短距离。

思路:
对于两个相同长度的向量,其距离按照时间为自变量构成一个二次函数。
那么对于两只狗每次取相同长度的直线来计算距离,然后推下去。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cmath>
using namespace std;const int maxn = 1e5 + 7;struct Point {double x,y;
}p1[maxn],p2[maxn];double dis(Point a,Point b) {return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}Point get(Point a,Point b,double l) { //获取向量(a,b)长度为l的一段Point ans;double len = dis(a,b);double ex = (b.x - a.x) / len,ey = (b.y - a.y) / len;ans.x = a.x + ex * l;ans.y = a.y + ey * l;return ans;
}double solve(Point a1,Point b1,Point a2,Point b2) { //计算最短距离double L1 = dis(a1,b1),L2 = dis(a2,b2);double ex1 = (b1.x - a1.x) / L1,ey1 = (b1.y - a1.y) / L1;double ex2 = (b2.x - a2.x) / L2,ey2 = (b2.y - a2.y) / L2;double a = (ex1 - ex2) * (ex1 - ex2) + (ey1 - ey2) * (ey1 - ey2);double b = 2 * ((a1.x - a2.x) * (ex1 - ex2) + (a1.y - a2.y) * (ey1 - ey2));double c = (a1.x - a2.x) * (a1.x - a2.x) + (a1.y - a2.y) * (a1.y - a2.y);if(a == 0) {return min(dis(a1,a2),dis(b1,b2));}double x = -b / (2 * a);if(x < 0 || x > L1) {return min(dis(a1,a2),dis(b1,b2));}return sqrt(a * x * x + b * x + c);
}int main() {int n;scanf("%d",&n);for(int i = 1;i <= n;i++) {scanf("%lf%lf",&p1[i].x,&p1[i].y);}int m;scanf("%d",&m);for(int i = 1;i <= m;i++) {scanf("%lf%lf",&p2[i].x,&p2[i].y);}int i = 2,j = 2;double mi = 1e9;while(i <= n && j <= m) {double l1 = dis(p1[i - 1],p1[i]),l2 = dis(p2[j - 1],p2[j]);if(fabs(l1 - l2) < 1e-8) {mi = min(mi,solve(p1[i - 1],p1[i],p2[j - 1],p2[j]));i++;j++;}else if(l1 < l2) {Point now = get(p2[j - 1],p2[j],l1);mi = min(mi,solve(p1[i - 1], p1[i], p2[j - 1], now));i++;p2[j - 1] = now;}else if(l1 > l2) {Point now = get(p1[i - 1],p1[i],l2);mi = min(mi,solve(p1[i - 1],now,p2[j - 1],p2[j]));j++;p1[i - 1] = now;}}printf("%.10f\n",mi);return 0;
}

这篇关于K - Keeping the Dogs Apart GYM-101550(计算几何)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907403

相关文章

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

XTU 1237 计算几何

题面: Magic Triangle Problem Description: Huangriq is a respectful acmer in ACM team of XTU because he brought the best place in regional contest in history of XTU. Huangriq works in a big compa

poj 3304 几何

题目大意:给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。 解题思路:如果存在这样的直线,过投影相交点(或投影相交区域中的点)作直线的垂线,该垂线(也是直线)必定与每条线段相交,问题转化为问是否存在一条直线和所有线段相交。 若存在一条直线与所有线段相交,此时该直线必定经过这些线段的某两个端点,所以枚举任意两个端点即可。

POJ 2318 几何 POJ 2398

给出0 , 1 , 2 ... n 个盒子, 和m个点, 统计每个盒子里面的点的个数。 const double eps = 1e-10 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;}struct Point{double x , y

poj 2653 几何

按顺序给一系列的线段,问最终哪些线段处在顶端(俯视图是完整的)。 const double eps = 1e-10 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;}struct Point{double x , y ;Point(){}Po

音视频入门基础:WAV专题(10)——FFmpeg源码中计算WAV音频文件每个packet的pts、dts的实现

一、引言 从文章《音视频入门基础:WAV专题(6)——通过FFprobe显示WAV音频文件每个数据包的信息》中我们可以知道,通过FFprobe命令可以打印WAV音频文件每个packet(也称为数据包或多媒体包)的信息,这些信息包含该packet的pts、dts: 打印出来的“pts”实际是AVPacket结构体中的成员变量pts,是以AVStream->time_base为单位的显

计算数组的斜率,偏移,R2

模拟Excel中的R2的计算。         public bool fnCheckRear_R2(List<double[]> lRear, int iMinRear, int iMaxRear, ref double dR2)         {             bool bResult = true;             int n = 0;             dou