Pipe - POJ 1039(线段相交交点)
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; const int MAXN = ;
const double EPS = 1e-;
const double oo = 1e9+; int Sign(double t)
{
if(t > EPS)return ;
if(fabs(t)<EPS)return ;
return -;
} struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y){}
Point operator - (const Point &t)const{
return Point(x-t.x, y-t.y);
}
double operator ^(const Point &t)const{
return x*t.y - y*t.x;
}
bool operator == (const Point &t)const{
return fabs(x-t.x)<EPS && fabs(y-t.y)<EPS;
}
};
struct Segment
{
Point S, E;
double a, b, c;///ax + by = c; Segment(Point S=, Point E=):S(S),E(E){
///求线段所在的直线的常数项
a = S.y - E.y;
b = E.x - S.x;
c = E.x*S.y - S.x*E.y;
}
bool Inters(const Segment &tmp) const{
int t1 = Sign((S-E)^(tmp.S-E));
int t2 = Sign((S-E)^(tmp.E-E));
///不存在平行,或者共线情况,有一点相交都算相交
if(t1+t2== || t1+t2==-)
return false;
else
return true;
}
Point crossNode(const Segment &tmp)const{
///求交点
Point t;
t.x = (c*tmp.b-tmp.c*b) / (a*tmp.b-tmp.a*b);
t.y = (c*tmp.a-tmp.c*a) / (b*tmp.a-tmp.b*a); return t;
}
void MakeNewSeg(double Lx, double Rx)
{///构造新的线段,与原来的线段共线
S.x = Lx, S.y = (c-a*Lx) / b;
E.x = Rx, E.y = (c-a*Rx) / b;
}
}; double FindMinX(Segment s, Segment sg[], int N)
{
double ans=oo; for(int i=; i<N; i++)
{
if(s.Inters(sg[i]))
continue; Segment t1(sg[i-].S, sg[i].S);
Segment t2(sg[i-].E, sg[i].E); if(s.Inters(t1))
{
Point tmp = s.crossNode(t1);
ans = tmp.x;
}
if(s.Inters(t2))
{
Point tmp = s.crossNode(t2); if(fabs(ans-oo) < EPS)
ans = tmp.x;
else
ans = max(ans, tmp.x);
} break;
} return ans;
} int main()
{
int N; while(scanf("%d", &N) != EOF && N)
{
Point p[MAXN];
Segment sg[MAXN]; int k=; for(int i=; i<N; i++, k+=)
{
scanf("%lf%lf", &p[k].x, &p[k].y);
p[k+].x = p[k].x, p[k+].y = p[k].y - 1.0;
sg[i] = Segment(p[k], p[k+]);
} double ans = -oo; for(int i=; i<k; i++)
for(int j=i+; j<k; j++)
{
if(fabs(p[i].x-p[j].x) < EPS)
continue; Segment s(p[i], p[j]);
s.MakeNewSeg(sg[].S.x, sg[N-].S.x); if(s.Inters(sg[]))
ans = max(ans, FindMinX(s, sg, N));
/// printf("i=%d, j=%d, ans=%f\n", i, j, ans);
} if(fabs(ans-oo) < EPS)
printf("Through all the pipe.\n");
else
printf("%.2f\n", ans);
} return ;
}
Pipe - POJ 1039(线段相交交点)的更多相关文章
- poj 1066 线段相交
链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- poj 1269 线段相交/平行
模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...
- poj 2653 线段相交
题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...
- poj 2653 线段相交裸题(解题报告)
#include<stdio.h> #include<math.h> const double eps=1e-8; int n; int cmp(double x) { if( ...
- poj 1410 线段相交判断
http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- HDU 2150 Pipe( 判断线段相交水 )
链接:传送门 题意:略 思路:数据量很小,直接暴力所有线段 /********************************************************************* ...
- Pick-up sticks - POJ 2653 (线段相交)
题目大意:有一个木棒,按照顺序摆放,求出去上面没有被别的木棍压着的木棍..... 分析:可以维护一个队列,如果木棍没有被压着就入队列,如果判断被压着,就让那个压着的出队列,最后把这个木棍放进队列, ...
- The Doors - POJ 1556 (线段相交)
题目大意:有一个房间(左上角(0,10),右下角(10,0)),然后房间里有N面墙,每面墙上都有两个门,求出来从初始点(0,5),到达终点(10,5)的最短距离. 分析:很明显根据两点之间直线最短 ...
- POJ1269 Intersecting Lines[线段相交 交点]
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15145 Accepted: 66 ...
随机推荐
- VS2008 未找到编译器可执行文件 csc.exe【当网上其他方法试玩了之后不起作用的时候再用这个方法】
被公司派遣到中国海洋石油惠州炼化公司做项目,做的是生产管理,来了发现他们的项目结构简直烂的要命,和同学们写的毕业设计差不多,然后开发工具用的是vs2008,我电脑是安装了vs2005和vs2010,v ...
- ios7--系统自带的向右滑动手势返回上一个界面
当从控制器A push到控制器B,我们返回控制器A,除了使用按钮返回 [self.navigationController pushViewController:Vc animated:YES]; 还 ...
- winform中的chat
百度一下 源代码下载:百度一下
- mahout分类
分类看起来比聚类和推荐麻烦多了 分类算法与聚类和推荐算法的不同:必须是有明确结果的,必须是有监督的,主要用于预测和检测 Mahout的优势 mahout的分类算法对资源的要求不会快于训练数据和测试数据 ...
- 理解MySQL——索引与优化(转)
写 在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页 面大小为4K,并存储100条记录.如果没有索引,查 ...
- 关于php的一些开源程序
最好用的当属thinksns,目前更新到4.3.4, 社交型网站. 此网站提供大量源码,有时间可以去看看:http://down.admin5.com/
- HttpWebRequest 写入报错
HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size ...
- python【第十二篇】Mysql基础
内容: 1.数据库介绍及MySQL简介 2.MySQL基本操作 1 数据库介绍 1.1什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同 ...
- Spring 初学 1
Spring是一个轻量级的框架,他有自己的MVC框架SpringMVC,在以往的Web项目中大多采用Structs2+hibernate+Spring的框架,Structs做web层,Hibernat ...
- 转: QtCreator调试程序时GDB崩溃
这个情况出现在QtCreator的2.5版以上,是由于新版QtCreator至少需要7.2 IIRC版的GDB.可以到:http://builds.qt-project.org/job/gdb-win ...