题目大意:有一个不反光并且不透光的管道,现在有一束光线从最左端进入,问能达到的最右端是多少,输出x坐标。
 
分析:刚开始做是直接枚举两个点然后和管道进行相交查询,不过这样做需要考虑的太多,细节不容易掌控。后来发现其实只需要对接口进行一下相交查询就简单多了,因为只需要考虑能不能通过每个截口就可以了,而且这样做的好处还有没有平行线和重叠线的情况,因为所有的截口都是垂直于x轴的,换一种想法海阔太空啊。
 
代码如下:
==========================================================================================================================
#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(线段相交交点)的更多相关文章

  1. poj 1066 线段相交

    链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  2. poj 1269 线段相交/平行

    模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...

  3. poj 2653 线段相交

    题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...

  4. poj 2653 线段相交裸题(解题报告)

    #include<stdio.h> #include<math.h> const double eps=1e-8; int n; int cmp(double x) { if( ...

  5. poj 1410 线段相交判断

    http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  6. HDU 2150 Pipe( 判断线段相交水 )

    链接:传送门 题意:略 思路:数据量很小,直接暴力所有线段 /********************************************************************* ...

  7. Pick-up sticks - POJ 2653 (线段相交)

    题目大意:有一个木棒,按照顺序摆放,求出去上面没有被别的木棍压着的木棍.....   分析:可以维护一个队列,如果木棍没有被压着就入队列,如果判断被压着,就让那个压着的出队列,最后把这个木棍放进队列, ...

  8. The Doors - POJ 1556 (线段相交)

    题目大意:有一个房间(左上角(0,10),右下角(10,0)),然后房间里有N面墙,每面墙上都有两个门,求出来从初始点(0,5),到达终点(10,5)的最短距离.   分析:很明显根据两点之间直线最短 ...

  9. POJ1269 Intersecting Lines[线段相交 交点]

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15145   Accepted: 66 ...

随机推荐

  1. MVC权限模块

    设计方向: 1.摒弃SiteMap,避免在容易书写错误的sitemap中书写,导航在controller和action上打标签生成. 2.controller统一继承basecontroller,在b ...

  2. Action<>和Func<>区别

    Action<>和Func<>其实都是委托的[代理]简写形式. 简单的委托写法: //普通的委托 public delegate void myDelegate(string ...

  3. ACM YTU 挑战编程 字符串 Problem A: WERTYU

    Problem A: WERTYU Description A common typing error is to place yourhands on the keyboard one row to ...

  4. ACM HDU 1021 Fibonacci Again

    #include<iostream> using namespace std; int main() { int n; while(cin>>n) { if((n+1)%4== ...

  5. Linux的cat、more、less的区别

    cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此经常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面在显示满一页时暂停,此时可按空格健继 ...

  6. JqGrid实现自定义查询

    $("#jqGridId").setGridParam({url:"数据查询地址"}).trigger("reloadGrid");

  7. Jquery Mobile下设置radio控件选中

    问题: .html文件头部引入了: <script src="js/jquery.js"></script> <script src="js ...

  8. Codeigniter-验证数据类

    个人需求,仿着CI的表单验证写了一个自己的验证类 1.定义验证类 <?php if ( ! defined('BASEPATH')) exit('No direct script access ...

  9. Java DecimalFormat数据格式化例子

    public static void main (String args[]) { DecimalFormat dFormat = new DecimalFormat(".##") ...

  10. 自动发布工具版本从python2升级成python3后遇到的种种问题(涉及paramiko,Crypto,zipfile等等)

    从在公司实习到正式入职,一直还在被同事使用的是我写的一个自动发布工具.该工具的主要功能是:开发人员给出需要更新的代码包(zip格式),测试人员将该代码包部署到测服,这些代码包和JIRA数据库里的项目信 ...