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 ...
随机推荐
- Cocos2dx边学边总结——开篇(一)
Cocos2dx是一个很好的开源跨平台2d游戏引擎,我们都知道他底层是基于OpenGl ES的,OpenGl 是跨平台的. 正是得益于这点 Cocos2dx的显示部分可以很好的跨平台运作,笔者认为 未 ...
- Windowsphone 之xml序列化和反序列化的应用(WebService解析返回的数据DataSet )
关于Xml的序列化和反序列化: 可以看这篇文章,http://www.cnblogs.com/Windows-phone/p/3243575.html WebService解析返回的数据DataSet ...
- [转]python yield
任何使用yield的函数都称之为生成器,如: def count(n): while n > 0: yield n #生成值:n n -= 1 另外一种说法:生成器就是一个返回迭代器的函数, ...
- VisualSVN Server 从此告别SVN记事本配置
http://www.visualsvn.com/downloads/ 注意下载的是Server版本,他还会提供一个visual Studio的插件: 安装完毕后,可以在管理界面进行角色添加,创建 ...
- JDK下sun.net.www.protocol.http.HttpURLConnection类-----Http客户端实现类的实现分析
HttpClient类是进行TCP连接的实现类, package sun.net.www.http; import java.io.*; import java.net.*; import java. ...
- UFLDL教程(六)之栈式自编码器
第0步:初始化一些参数和常数 第1步:利用训练样本集训练第一个稀疏编码器 第2步:利用训练样本集训练第二个稀疏编码器 第3步:利用第二个稀疏编码器提取到的特征训练softmax回归模型 ...
- CSS3随笔系列之transform(一)—— transform-origin
transform-origin属性平时似乎用得很少,它决定了变换时依赖的原点.基本的属性特性可以参考CSS手册. 如果在H5动画项目中,用到旋转的话,它还是不能小觑的. 假如我们做一个秋千效果 其实 ...
- 在线浏览pdf文件,pdfobject的简单使用
该js插件,官网有详细的使用教程(网址:http://www.pdfobject.com/examples/).打开里面的例子后,查看新打开页面,打开并查看该页面的源代码. 需要的材料: 1.PDFo ...
- 【POJ】2117 Electricity
无向图求割点和连通块. /* POJ2117 */ #include <iostream> #include <vector> #include <algorithm&g ...
- openwrt使用3G上网卡
尊敬的大大.感谢你抽空指导我 我的设备是db120 mu350 和广东无限卡 版本是OpenWrt Backfire 10.03.336 DIY full 一. 没有安装到kmod-us ...