poj3924
题目:给定一个起点(xw1, yw1),直线经过(xw2, yw2),速度为vw无限运动的点,还有一个起点(xt1, yt1),终点(xt2, yt2),并且在以vt速度在两者往返运动,求两者在运动中的最近距离。。如果小于给定的dl,输出Dangerous,大于du输出Miss,否则输出perfect。。
思路:
ACM2008北京赛区的计算几何题,也是dyf神犇violet系列的一道题。。
思路不是很难,以xw点为参考系,那么题目就变成了求某个点及一些折线的最近距离。。
而且这些折线可以分成两组平行线段,每组可以分别求。
至于怎么求,可以用dyf神犇的数学方法。。(可能我太弱了,写完后一直错挑了半天就直接弃疗了)
我最后用的是直接三分线段,写起来好写一点。。
有什么不明白的可以看dyf博客。。写的很清楚。。
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
#include<utility>
#define M0(x) memset(x, 0, sizeof(x))
#define eps 1e-8
#define pi acos(-1.0)
using namespace std;
inline int sgn(const double& x){
return (x > eps) - (x < -eps);
}
struct point{
double x, y;
point(){}
point(double _x, double _y):x(_x), y(_y){}
void input(){
scanf("%lf%lf", &x, &y);
}
double len()const{
return sqrt(x * x + y * y);
}
point trunc(double l)const{
double r = l / len();
return point(r * x, r * y);
}
point rotate_left()const{
return point(-y, x);
}
point operator-(const point& p1)const{
return point(x - p1.x, y - p1.y);
}
point operator+(const point& p1)const{
return point(x + p1.x, y + p1.y);
}
bool operator==(const point& p1)const{
return sgn(x - p1.x) == && sgn(y - p1.y) == ;
}
double operator*(const point& p1)const{
return x * p1.y - y * p1.x;
}
double operator^(const point& p1)const{
return x * p1.x + y * p1.y;
}
point operator*(const double& l) const{
return point(x *l , y * l);
}
void out(){
printf("%.2f %.2f\n", x, y);
}
} p[], p2[], v[], zero(, );
double di, du, vnow; double get_distance(const point &p, const point &p1, const point &p2){
if (sgn((p2 - p1) ^ (p - p1)) <= ) return (p - p1).len();
if (sgn((p1 - p2) ^ (p - p2)) <= ) return (p - p2).len();
return fabs((p1 - p) * (p2 - p)) / (p1 - p2).len();
} void init(){
p2[].input(), scanf("%lf", &vnow);
v[] =(p2[]-p[]).trunc(vnow);
p[].input(), p2[].input(), scanf("%lf", &vnow);
v[] = (p2[] - p[]).trunc(vnow);
p[] = p[] - p[], p2[] = p2[] - p[];
scanf("%lf%lf", &di, &du);
} double gao(const point& p0,const point& p1, const point& p2){
int l = , r = 0x3fffffff, ui, l1, r1;
point v = p2 - p0;
point p00, p11;
double dis1, dis2, ans = 1e20;
while (l + < r){
if (l + >= r){
ans = min(ans, get_distance(zero, p0 + v * l, p1 + v * l));
ans = min(ans, get_distance(zero, p0 + v * r, p1 + v * r));
if (l + == r)
ans = min(ans, get_distance(zero, p0 + v * (l+), p1 + v * (l+)));
break;
}
ui = (r - l + ) / ;
l1 = l + ui, r1 = r - ui;
dis1 = get_distance(zero, p0 + v * l1, p1 + v * l1);
dis2 = get_distance(zero, p0 + v * r1, p1 + v * r1);
if (dis1 < dis2) r = r1;
else l = l1; }
return ans;
} void solve(){
point v1 = v[] - v[], v2 = (v[] + v[]) * (-);
double t = (p2[] - p[]).len() / vnow;
point p0 = p[], p1 = p0 + v1 * t, p3 = p1 + v2 * t, p4 = p3 + v1 * t;
// p0.out(), p1.out(), p3.out(), p4.out();
double ans = gao(p0, p1, p3);
ans = min(gao(p1, p3, p4), ans);
if (ans < di - eps) puts("Dangerous");
else if (ans > du + eps) puts("Miss");
else puts("Perfect");
} int main(){
while (scanf("%lf%lf", &p[].x, &p[].y) != EOF){
init();
solve();
}
return ;
}
poj3924的更多相关文章
随机推荐
- iOS.FBTweak
FBTweak的源码分析 1. FBTweak提供了以下功能 A): 可以动态的修改某个变量的值,这些变量的类型包括: ... B): 可以以plist的形式将Tweak以key-value的形式进行 ...
- iOS.XcodeUsage
1. Customizing Xcode File Templates http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa- ...
- ORM学员管理系统单表查询示例
前期准备工作 首先创建好一个项目 一:必须使用MySQL创建一个库 因为ORM只能对表和数据进行处理,所以库必须自己创建 create database mysite; 二:进行相关的配置 在项目my ...
- 各种平台的表达芯片跟mRNA-seq数据比较
各种平台的表达芯片跟mRNA-seq数据比较 RNA-Seq 表达谱 芯片数据分析 文章见:http://journals.plos.org/plosone ... ournal.pone.00786 ...
- vim删除单词
参考资料: https://blog.csdn.net/grey_csdn/article/details/72355735 混迹于Windows.Linux以及Mac,选择加强自己的VIM水平应该不 ...
- Netty 源码 Channel(二)主要类
Netty 源码 Channel(二)主要类 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 一.Channel 类图 二. ...
- Null value was assigned to a property of primitive type setter of cn.itcast.oa.domain.Forum.topicCount
[引用http://m.blog.csdn.net/blog/u013998070/41087351] Null value was assigned to a property of primiti ...
- 重新学pytorch
安装: conda install pytorch -c pytorch pip3 install torchvision pip install torchtext 这3个命令就够了
- C# 遇到 which has a higher version than referenced assembly
当C#遇到这种提示: which has a higher version than referenced assembly, 说明有两个或多个工程引用的dll的版本有出现不一样, 如: A工程引用l ...
- Flex的Number和Text
今天要说的问题不是Number和String转换的问题.而是使用时容易出的一些错误: public static function ToFixed(value:Number, digits:uint ...