UVA_303_Pipe_(计算几何基础)
描述
一个管道,有n个拐点(包括起点),给出每个拐点的上面的坐标,下面的横坐标与上面相同,总坐标-1.一道光线从起点口射入,问最多射到x=?的位置碰壁.
303 - Pipe
Time limit: 3.000 seconds
Pipe |
The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem of determining how far the light can reach inside each component of the pipe. Note that the material which the pipe is made from is not transparent and not light reflecting.
Each pipe component consists of many straight pipes connected tightly together. For the programming purposes, the company developed the description of each component as a sequence of points , where
. These are the upper points of the pipe contour. The bottom points of the pipe contour consist of points with y-coordinate decreased by 1. To each upper point
there is a corresponding bottom point
(see picture above). The company wants to find, for each pipe component, the point with maximal x-coordinate that the light will reach. The light is emitted by a segment source with endpoints
and
(endpoints are emitting light too). Assume that the light is not bent at the pipe bent points and the bent points do not stop the light beam.
Input
The input file contains several blocks each describing one pipe component. Each block starts with the number of bent points on separate line. Each of the next n lines contains a pair of real values
separated by space. The last block is denoted with n = 0.
Output
The output file contains lines corresponding to blocks in input file. To each block in the input file there is one line in the output file. Each such line contains either a real value, written with precision of two decimal places, or the message Through all the pipe.. The real value is the desired maximal x-coordinate of the point where the light can reach from the source for corresponding pipe component. If this value equals to , then the message Through all the pipe. will appear in the output file.
Sample Input
4
0 1
2 2
4 1
6 4
6
0 1
2 -0.6
5 -4.45
7 -5.57
12 -10.8
17 -16.55
0
Sample Output
4.67
Through all the pipe.
分析
抄黑书:
1.如果某一道光没有擦过顶点,可以平移使解更优(碰到一个顶点).
2.如果某一道光只擦过一个顶点,可以旋转使解更优(碰到另一个顶点).
3.如果某一道光没有碰到上下各一个顶点,则可以绕前面或后面的点旋转,使解更优(碰到上下各一个顶点).
4.如果某一道光碰到了上下各一个顶点,则只有一个方向可以旋转,如果向该方向旋转可以使解更优,则旋转,直到再次碰到两个点.这两个点或者一上一下或者在同一方向,继续重复上述过程,直到第4步唯一能够旋转的方向不能使得解更优.
如此一来解法就明了了:因为最终的最优解一定是一上一下两个顶点的连线,所以枚举一上一下的点对,求每条直线的最优解即可.
注意:
1.无论是否时规范相交都要算交点,因为可能是撞在了拐点处,不规范相交的唯一交点求法相同(稍微推到一下就好了).
2.如果光线每通过i,在i-1的边找的时候即要找上面的也要找下面的,因为可能是擦过了上面,撞在了下面,或者擦过了下面,撞在了上面.
#include <bits/stdc++.h>
using namespace std; const int maxn=+;
const double eps=1e-;
int n;
double ans; struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
}up[maxn],down[maxn];
typedef Point Vector;
Vector operator - (Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
inline int dcmp(double x){
if(fabs(x)<eps) return ;
return x>?:-;
}
inline double cross(Point a,Point b){
return a.x*b.y-b.x*a.y;
}
inline bool segment_cross(Point a,Point b,Point c,Point d,Point &p){
double s1,s2;
int d1,d2;
d1=dcmp(s1=cross(b-a,c-a)),d2=dcmp(s2=cross(b-a,d-a));
if(d1*d2>) return false;
else
p=Point((c.x*s2-d.x*s1)/(s2-s1),(c.y*s2-d.y*s1)/(s2-s1));//无论是否是规范相交,都要算交点
return true;
}
void ray(Point a,Point b){
Point c;
if(!segment_cross(a,b,up[],down[],c)) return;
for(int i=;i<=n;i++){
if(!segment_cross(a,b,up[i],down[i],c)){//如果通过了拐口i,那么一定和up[i],down[i]所连直线相交
segment_cross(a,b,up[i-],up[i],c);//如果没有通过,就在前一条的上面和下面找交点
ans=max(ans,c.x);
segment_cross(a,b,down[i-],down[i],c);
ans=max(ans,c.x);
return;
}
}
ans=up[n].x;
}
void solve(){
ans=up[].x;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)//枚举每个一上一下的点对
if(i!=j) ray(up[i],down[j]);
if(ans==up[n].x) puts("Through all the pipe.");
else printf("%.2lf\n",ans);
}
int main(){
while(scanf("%d",&n)&&n){
for(int i=;i<=n;i++){
scanf("%lf%lf",&up[i].x,&up[i].y);
down[i].x=up[i].x; down[i].y=up[i].y-;
}
solve();
}
return ;
}
UVA_303_Pipe_(计算几何基础)的更多相关文章
- nyis oj 68 三点顺序 (计算几何基础)
三点顺序 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 如今给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,如今让你推断A,B,C是顺时针给出的还是逆 ...
- 【BZOJ】1043: [HAOI2008]下落的圆盘(计算几何基础+贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1043 唯一让我不会的就是怎么求圆的周长并QAAQ... 然后发现好神!我们可以将圆弧变成$[0, 2 ...
- 计算几何基础——矢量和叉积 && 叉积、线段相交判断、凸包(转载)
转载自 http://blog.csdn.net/william001zs/article/details/6213485 矢量 如果一条线段的端点是有次序之分的话,那么这种线段就称为 有向线段,如果 ...
- BZOJ_1610_[Usaco2008_Feb]_Line连线游戏_(计算几何基础+暴力)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1610 给出n个点,问两两确定的直线中,斜率不同的共有多少条. 分析 暴力枚举直线,算出来斜率放 ...
- 二维计算几何基础题目泛做(SYX第一轮)
题目1: POJ 2318 TOYS 题目大意: 给一个有n个挡板的盒子,从左到右空格编号为0...n.有好多玩具,问每个玩具在哪个空格里面. 算法讨论: 直接叉积判断就可以.注意在盒子的边界上面也算 ...
- 计算几何基础算法几何C++实现
This file is implementation of Common Common Computational Geometry Algorithms.Please please pay att ...
- 【POJ】1556 The Doors(计算几何基础+spfa)
http://poj.org/problem?id=1556 首先路径的每条线段一定是端点之间的连线.证明?这是个坑...反正我是随便画了一下图然后就写了.. 然后re是什么节奏?我记得我开够了啊.. ...
- 【POJ】2318 TOYS(计算几何基础+暴力)
http://poj.org/problem?id=2318 第一次完全是$O(n^2)$的暴力为什么被卡了-QAQ(一定是常数太大了...) 后来排序了下点然后单调搞了搞..(然而还是可以随便造出让 ...
- 【POJ】2653 Pick-up sticks(计算几何基础+暴力)
http://poj.org/problem?id=2653 我很好奇为什么这样$O(n^2)$的暴力能过.... 虽然说这是加了链表优化的,但是最坏不也是$O(n^2)$吗...(只能说数据太弱.. ...
随机推荐
- c语言学习之基础知识点介绍(十三):枚举的介绍和使用
一.枚举的介绍 /* 枚举:限制的待选项. 语法: enum 枚举名{ 选项1, 选项2, 选项3, ........ 选项n }; 注意:枚举中,选项之间用 , 隔开,最后一个不用加 , :并且枚举 ...
- 使用PDO持久化连接
无论是何种编程语言,几乎都要经常与各种数据库打交道.不过,众所周知的是,在程序与数据库之间建立连接是一件比较耗费资源的事情,因此编程技术领域的许多专家.前辈们就设想并提出了各种解决方案,以减少不必要的 ...
- c#对象初始化
class test:IEquatable<test> { public int aa { get; set; } public string bb { get; set; } publi ...
- Druid 简单介绍
官方网址:http://code.alibabatech.com/wiki/display/Druid/Home 1.什么是Druid Druid首先是一个数据库连接池.Druid是目前最好的数据库连 ...
- 重新设置MySQL的root密码
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...
- 幾種方法實現C語言Macro for debug
1. #include <stdio.h> #include <stdlib.h> #define DEBUG 1 #ifdef DEBUG #define DEBUG_PRI ...
- Java进程CPU使用率高排查
Java进程CPU使用率高排查 生产java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下.1.jps 获取Java进程的PID.2.jstack pid >> ...
- Linux文件权限学习总结
一.用户对文件或目录都有哪些权限? 四种:读.写.执行.没有权限 二.如何表示这四种权限? 如果用十进制数字表示,分别为:4.2.1.0:如果用字符表示,分别为:r.w.x.-.个人觉得,使用chmo ...
- ubuntu ll命令
用过 Redhat 的朋友应该很熟悉 ll 这个命令,就相当于 ls -l,但在 Ubuntu 中就不行了.严格来说 ll 不是一个命令,只是命令的别名而已.很多 Linux 用户都使用 bash s ...
- IE6 png 透明 (三种解决方法)
FF和IE7已经直接支持透明的png图了,下面这个主要是解决IE6下透明PNG图片有灰底的 ====================================================== ...