poj 1039 Pipe (Geometry)
理解错题意一个晚上。_(:з」∠)_
题意很容易看懂,就是要求你求出从外面射进一根管子的射线,最远可以射到哪里。
正解的做法是,选择上点和下点各一个,然后对于每个折点位置竖直位置判断经过的点是否在管中。如果是,就继续找,如果不在管中,这时射线必然已经穿过管出去了,这时就要找射线和管上下壁的交点。
代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath> using namespace std; const double EPS = 1e-;
const int N = ;
inline int sgn(double x) { return (x > EPS) - (x < -EPS);}
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
bool operator < (Point a) const { return sgn(x - a.x) < || sgn(x - a.x) == && y < a.y;}
bool operator == (Point a) const { return sgn(x - a.x) == && sgn(y - a.y) == ;}
Point operator + (Point a) { return Point(x + a.x, y + a.y);}
Point operator - (Point a) { return Point(x - a.x, y - a.y);}
Point operator * (double p) { return Point(x * p, y * p);}
Point operator / (double p) { return Point(x / p, y / p);}
} ;
typedef Point Vec;
inline double dot(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
inline double cross(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
inline double veclen(Vec x) { return sqrt(dot(x, x));}
inline Point vecunit(Vec x) { return x / veclen(x);}
inline Point normal(Vec x) { return Point(-x.y, x.x) / veclen(x);} struct Line {
Point s, t;
Line() {}
Line(Point s, Point t) : s(s), t(t) {}
Vec vec() { return t - s;}
Point point(double p) { return s + vec() * p;}
} ; Point up[N], dw[N];
Line ul[N], dl[N]; inline Point llint(Point P, Vec u, Point Q, Vec v) { return P + u * (cross(v, P - Q) / cross(u, v));}
bool tstcross(Point a, Point b, Point c, Point d) { return sgn(cross(a - c, b - c)) * sgn(cross(a - d, b - d)) > ;} double cal(Point s, Point t, int n) {
Line tl = Line(s, t);
if (tstcross(tl.s, tl.t, up[], dw[])) return -1e100;
for (int i = ; i < n - ; i++) {
if (tstcross(tl.s, tl.t, up[i + ], dw[i + ])) {
double ret = -1e100;
if (!tstcross(tl.s, tl.t, ul[i].s, ul[i].t)) {
Point tp = llint(tl.s, tl.vec(), ul[i].s, ul[i].vec());
ret = max(ret, tp.x);
}
if (!tstcross(tl.s, tl.t, dl[i].s, dl[i].t)) {
Point tp = llint(tl.s, tl.vec(), dl[i].s, dl[i].vec());
ret = max(ret, tp.x);
}
return ret;
}
}
return 1e100;
} void work(int n) {
for (int i = ; i < n - ; i++) {
ul[i] = Line(up[i], up[i + ]);
dl[i] = Line(dw[i], dw[i + ]);
}
double ans = -1e100;
for (int i = ; i < n; i++) {
for (int j = i + ; j < n; j++) {
if (ans >= 1e99) break;
ans = max(ans, cal(up[i], dw[j], n));
ans = max(ans, cal(dw[i], up[j], n));
}
}
if (ans >= 1e99) puts("Through all the pipe.");
else printf("%.2f\n", ans);
} int main() {
// freopen("in", "r", stdin);
int n;
while (~scanf("%d", &n) && n) {
for (int i = ; i < n; i++) {
scanf("%lf%lf", &up[i].x, &up[i].y);
dw[i] = Point(up[i].x, up[i].y - 1.0);
}
work(n);
}
return ;
}
——written by Lyon
poj 1039 Pipe (Geometry)的更多相关文章
- poj 1039 Pipe(叉乘。。。)
题目:http://poj.org/problem?id=1039 题意:有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从 ...
- POJ - 1039 Pipe(计算几何)
http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...
- POJ 1039 Pipe【经典线段与直线相交】
链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 简单几何(直线与线段相交) POJ 1039 Pipe
题目传送门 题意:一根管道,有光源从入口发射,问光源最远到达的地方. 分析:黑书上的例题,解法是枚举任意的一个上顶点和一个下顶点(优化后),组成直线,如果直线与所有竖直线段有交点,则表示能穿过管道. ...
- POJ 1039 Pipe(直线和线段相交判断,求交点)
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8280 Accepted: 2483 Description ...
- POJ 1039 Pipe
题意:一根管子,中间有一些拐点,给出拐点的上坐标,下坐标为上坐标的纵坐标减1,管子不能透过光线也不能折射光线,问光线能射到最远的点的横坐标. 解法:光线射到最远处的时候一定最少经过两个拐点,枚举每两个 ...
- poj 1039 Pipe(几何基础)
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9932 Accepted: 3045 Description ...
- POJ 1039 Pipe 枚举线段相交
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9493 Accepted: 2877 Description ...
- POJ 1039 Pipe | 线段相交
题目: 给一个管子,有很多转弯处,问从管口的射线射进去最长能射到多远 题解: 根据黑书,可以证明的是这条光线一定经过了一个上顶点和下顶点 所以我们枚举每对上下顶点就可以了 #include<cs ...
随机推荐
- Lab2 内存管理(实现细节)
lab2 中的变动 bootloader 的入口发生了改变 bootloader不像lab1那样,直接调用kern_init函数,而是先调用位于lab2/kern/init/entry.S中的kern ...
- JS 获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...
- Go开发 之 Go如何引用github包
- 转: CentOS上安装LAMP之第二步:PHP环境及安装过程报错解决方案(纯净系统环境)
最近有空就配置CentOS系统上的AMP环境,现在配置到PHP环境了 多话不说上传送门:http://blog.csdn.net/zhangatle/article/details/77447653 ...
- [Vue CLI 3] 插件开发之 registerCommand 到底做了什么
首先,我们看到在 package.json 中有 scripts 的定义: "scripts": { "serve": "vue-cli-servic ...
- oracle-Oradim
输入以下命令之一: 通过指定以下选项创建实例: -NEW -SID sid | -SRVC srvc | -ASMSID sid | -ASMSRVC srvc [-SYSPWD pass] [-ST ...
- Leetcode720.Longest Word in Dictionary词典中最长的单词
给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...
- 深入理解 Node.js 进程与线程
原文链接: https://mp.weixin.qq.com/s?__biz=MzAxODE2MjM1MA==&mid=2651557398&idx=1&sn=1fb991da ...
- 使用 Windows 10 WSL 搭建 ESP8266 编译环境并使用 VSCODE 编程(一)(2019-08-23)
目录 使用 Windows 10 WSL 搭建 ESP8266 编译环境并使用 VSCODE 编程 安装前准备 安装 ESP8266 工具链 下载 ESP8266 SDK 编译 花絮 使用 Windo ...
- 连接池dbcp
连接池dbcp DBCP:apache组织 使用步骤: 1.导入jar包(commons-dbcp-1.4.jar和commons-pool-1.5.6.jar.commons-logging-1.2 ...