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 ...
随机推荐
- 月薪10K必备--C#下拉框联动
下拉框联动 很多网站上都用到下拉框联动,就是第一个下拉框没有选择任何项,第二个下拉框就没有选项.这样的做法更加谨慎,更加紧密. 下面我就教大家怎么做下拉框联动: 首先在窗 ...
- POJ 1631 Bridging signals(LIS O(nlogn)算法)
Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...
- Windows phone 之Xml操作
最近在做天气预报应用,这个应用中需要用到Xml来存储关注的城市列表,保存一下代码,方便以后使用,也给博友们一个参考: 其中:添加关注城市的操作代码是: 其实就是, (1)先把数据从CareCityCo ...
- Xcode-01ARC / Block
1.nonatomic 2.assign 3.strong 4.weak 5.instancetype 6.@class @property 使部分类在编译时不使用ARC -(可以让这们支持 reta ...
- ARM920T系统总线时序分析
一.系统总线时序图 二.分析 第一个时钟周期开始,系统地址总线给出需要访问的存储空间地址. 经过Tacs时间后,片选信号也相应给出,并且锁存当前地址线上地址信息. 再经过Tcso时间后,处理器给出当前 ...
- Python/Keras如何将给定的数据集打乱
给定数据集data,数据集对应的标签label index = [i for i in range(len(data))] random.shuffle(index) data = data[inde ...
- python之requests-multipart/from-data
示例代码:files = {"token":(None,token), "key":(None,key), "file":"hel ...
- Codeforces Round #207 (Div. 2)
A:超级大水题: 代码: #include<cstdio> #define maxn 105 using namespace std; int n,a[maxn],x,y,ans; int ...
- MVC自学系列之三(MVC视图-Views)
View的约定 1.根据约定,Views目录下包含着每一个与Controller同名但是没有像Controller后缀的文件夹:因此对于控制器HomeController就对应在views目录下有个目 ...
- qt5使用curl实现文件下载的示例程序 good
http://blog.csdn.net/xueyushenzhou/article/details/51702672#t3 http://download.csdn.net/detail/xueyu ...