含【求直线交点】、【判断直线与线段相交】模板
 
Pipe
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:11940   Accepted: 3730

Description

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 [x1; y1], [x2; y2], . . ., [xn; yn], where x1 < x2 < . . . xn . 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 [xi; yi] there is a corresponding bottom point [xi; (yi)-1] (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 [x1; (y1)-1] and [x1; y1] (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 2 <= n <= 20 on separate line. Each of the next n lines contains a pair of real values xi, yi 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 xn, 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.

Source

题意:

给n个点 构成两条平行的折线

问从管道口出发的光线最远能到达的横坐标

思路:

最远的光线一定是贴着管道的某两个端点走的

现在枚举这两个端点  判断其与后面折线的交点

刚开始没想到判断交点时 可以先判断line 和 line(up[k], down[k])

这样得到的k就是最小的不能达到的k

用这个k就可以拿来算line 和 line(up[k-1], up[k])以及line(down[k -1], down[k])的交点了

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring> using namespace std;
typedef long long int LL; #define zero(x) (((x) > 0? (x) : -(x)) < eps)
const double eps = 1e-;
int sgn(double x)
{
if(fabs(x) < eps) return ;
if(x < ) return -;
else return ;
} struct point{
double x, y;
point(){}
point(double _x, double _y)
{
x = _x;
y = _y;
}
point operator -(const point &b)const
{
return point(x - b.x, y - b.y);
}
double operator ^(const point &b)const
{
return x * b.y - y * b.x;
}
double operator *(const point &b)const
{
return x * b.x + y * b.y;
}
void input()
{
scanf("%lf%lf", &x, &y);
}
}; struct line{
point s, e;
line(){}
line(point _s, point _e)
{
s = _s;
e = _e;
}
//0表示直线重合,1表示平行,2相交
pair<int, point>operator &(const line &b)const
{
point res = s;
if(sgn((s - e) ^ (b.s - b.e)) == ){
if(sgn((s - b.e) ^ (b.s - b.e)) == ){
return make_pair(, res);
}
else return make_pair(, res);
}
double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
res.x += (e.x - s.x) * t;
res.y += (e.y - s.y) * t;
return make_pair(, res);
}
}; //判断直线与线段相交
bool seg_inter_line(line l1, line l2)
{
return sgn((l2.s - l1.e) ^ (l1.s - l1.e)) * sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= ;
} int n;
point up[], down[];
int main()
{
while(scanf("%d", &n) != EOF && n != ){
for(int i = ; i < n; i++){
up[i].input();
down[i].x = up[i].x;
down[i].y = up[i].y - 1.0;
} bool flag = false;
double ans = -100000000.0;
int k;
for(int i = ; i < n; i++){
for(int j = i + ; j < n; j++){
for(k = ; k < n; k++){
if(seg_inter_line(line(up[i], down[j]), line(up[k], down[k])) == ){
break;
}
}
if(k >= n){
flag = true;
break;
}
if(k > max(i, j)){
if(seg_inter_line(line(up[i], down[j]), line(up[k - ], up[k]))){
pair<int, point>pr = line(up[i], down[j]) & line(up[k - ], up[k]);
point p = pr.second;
ans = max(ans, p.x);
}
if(seg_inter_line(line(up[i], down[j]), line(down[k - ], down[k]))){
pair<int, point>pr = line(up[i], down[j]) & line(down[k - ], down[k]);
point p = pr.second;
ans = max(ans, p.x);
}
} for(k = ; k < n; k++){
if(seg_inter_line(line(down[i], up[j]), line(up[k], down[k])) == ){
break;
}
}
if(k >= n){
flag = true;
break;
}
if(k > max(i, j)){
if(seg_inter_line(line(down[i], up[j]), line(up[k - ], up[k]))){
pair<int, point>pr = line(down[i], up[j]) & line(up[k - ], up[k]);
point p = pr.second;
ans = max(ans, p.x);
}
if(seg_inter_line(line(down[i], up[j]), line(down[k - ], down[k]))){
pair<int, point>pr = line(down[i], up[j]) & line(down[k - ], down[k]);
point p = pr.second;
ans = max(ans, p.x);
}
}
}
if(flag){
break;
}
}
//cout<<ans<<endl;
if(flag){
printf("Through all the pipe.\n");
}
else{
printf("%.2f\n", ans);
}
}
return ;
}

poj1039 Pipe【计算几何】的更多相关文章

  1. poj1039 Pipe(计算几何叉积求交点)

    F - Pipe Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  2. POJ1039 Pipe

    嘟嘟嘟 大致题意:按顺序给出\(n\)个拐点表示一个管道,注意这些点是管道的上端点,下端点是对应的\((x_i, y_i - 1)\).从管道口射进一束光,问能达到最远的位置的横坐标.若穿过管道,输出 ...

  3. POJ 1039:Pipe 计算几何

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9773   Accepted: 2984 Description ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  6. hdoj Pipe&&南阳oj管道问题&&poj1039(计算几何问题...枚举)

    Pipe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. Pipe(点积叉积的应用POJ1039)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9723   Accepted: 2964 Description ...

  8. 【计算几何初步-代码好看了点线段相交】【HDU2150】Pipe

    题目没什么 只是线段相交稍微写的好看了点 Pipe Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  9. POJ - 1039 Pipe(计算几何)

    http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...

随机推荐

  1. C 字符串操作函数

    针对C风格的字符串(char p[n];): 长度(strlen).追加(strcat, strncat).比较(strcmp, strncmp).查找(strchr, strstr)等. --带n的 ...

  2. Resource接口,及资源

    Resource介绍 编码的时候,除了代码本身,我们还需要对外部的资源进行处理.例如:URL资源.URI资源.File资源.ClassPath相关资源.服务器相关资源(VFS等)等等. 而这些资源的处 ...

  3. motion的移植和使用

    说明: motion主页:http://www.lavrsen.dk/foswiki/bin/view/Motion motion下载地址:http://sourceforge.net/project ...

  4. “Chaos”的算法之Floyd算法

    倘若我们要在计算机上建立一个交通咨询系统则可以采用图的结构来表示实际的交通网络.其实现最基本的功能,求出任意两点间的最短路径, 求最短路径的经典方法有很多种,最常用的便是迪杰斯特拉算法和佛洛依德(Fl ...

  5. 截取scrollview的滑动事件,传递给子控件

    重写一个ScrollView public class MyScrollView extends ScrollView{ public MyScrollView(Context context, At ...

  6. why pure virtual function has definition 为什么可以在基类中实现纯虚函数

    看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...

  7. C#委托和事件详解

    委托Delegate delegate是C#中的一种类型,它实际上是一个能够持有对某个方法的引用的类.与其它的类不同,delegate类能够拥有一个签名(signature),并且它"只能持 ...

  8. idea 新建项目 文件名都是红色的处理办法

    原因是当前的project用了版本控制器 所以这个project下面所有的项目都加入版本控制器里了,所以项目文件和名称都是红色的 简单文字叙述解决办法 file-->settings--> ...

  9. [转]ASP.NET MVC 5 -从控制器访问数据模型

    在本节中,您将创建一个新的MoviesController类,并在这个Controller类里编写代码来取得电影数据,并使用视图模板将数据展示在浏览器里. 在开始下一步前,先Build一下应用程序(生 ...

  10. string 线程安全

    线程安全:class Program { public delegate void MyDelegate(string aa); static void Main(string[] args) { M ...