POJ 1039 Pipe | 线段相交
题目:
给一个管子,有很多转弯处,问从管口的射线射进去最长能射到多远
题解:
根据黑书,可以证明的是这条光线一定经过了一个上顶点和下顶点
所以我们枚举每对上下顶点就可以了
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-5
using namespace std;
bool dcmp(double x,double y)
{
if (fabs(x-y)>eps) return 1;
return 0;
}
struct point
{
double x,y;
point () {};
point (double _x,double _y)
{
x=_x,y=_y;
}
point operator + (const point &a)const
{
return point(x+a.x,y+a.y);
}
point operator - (const point &a) const
{
return point(x-a.x,y-a.y);
}
double operator * (const point &a)const
{
return x*a.y-a.x*y;
}
double dot (const point &a,const point &b)
{
return a.x*b.x+a.y*b.y;
}
bool operator < (const point &a)const
{
return x<a.x;
}
bool operator == (const point &a)const
{
return dcmp(x,a.x) && dcmp(y,a.y);
}
}up[105],down[105];
inline double Max(double a,double b)
{
return a>b?a:b;
}
double Multi(point a,point b,point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool Across(point a,point b,point c,point d)
{
double tmp=Multi(c,b,a)*Multi(d,b,a);
if (tmp<0 || fabs(tmp)<eps) return 1;
return 0;
}
double getIntersect(point a,point b,point c,point d)
{
double A1=b.y-a.y,B1=a.x-b.x,C1=(b.x-a.x)*a.y-(b.y-a.y)*a.x;
double A2=d.y-c.y,B2=c.x-d.x,C2=(d.x-c.x)*c.y-(d.y-c.y)*c.x;
return (C2*B1-C1*B2)/(A1*B2-A2*B1);
}
int n,flag;
double ans;
int main()
{
while (scanf("%d",&n) && n)
{
for (int i=0;i<n;i++)
{
scanf("%lf%lf",&up[i].x,&up[i].y);
down[i].x=up[i].x;
down[i].y=up[i].y-1;
}
ans=up[0].x;
flag=0;
for (int i=0;i<n && !flag;i++)
for (int j=0;j<n && !flag;j++)
if (i!=j)
{
int k;
for (k=0;k<n;k++)
if (!Across(up[i],down[j],up[k],down[k])) break;
if (k==n) flag=1;
else if (k>i && k>j)
{
double tmp;
tmp=getIntersect(up[i],down[j],up[k-1],up[k]);
// printf("%.2f\n",tmp);
if (tmp>ans) ans=tmp;
tmp=getIntersect(up[i],down[j],down[k-1],down[k]);
// printf("%.2f\n",tmp);
if (tmp>ans) ans=tmp;
}
}
if (flag) puts("Through all the pipe.");
else printf("%.2f\n",ans);
}
return 0;
}
POJ 1039 Pipe | 线段相交的更多相关文章
- [poj 1039]Pipes[线段相交求交点]
题意: 无反射不透明管子, 问从入口射入的所有光线最远能到达的横坐标. 贯穿也可. 思路: 枚举每一组经过 up [ i ] 和 down [ j ] 的直线, 计算最远点. 因为无法按照光线生成的方 ...
- POJ 1039 Pipe【经典线段与直线相交】
链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 简单几何(直线与线段相交) POJ 1039 Pipe
题目传送门 题意:一根管道,有光源从入口发射,问光源最远到达的地方. 分析:黑书上的例题,解法是枚举任意的一个上顶点和一个下顶点(优化后),组成直线,如果直线与所有竖直线段有交点,则表示能穿过管道. ...
- POJ 1039 Pipe(直线和线段相交判断,求交点)
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8280 Accepted: 2483 Description ...
- POJ - 1039 Pipe(计算几何)
http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...
- poj 1066(枚举+线段相交)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6328 Accepted: 2627 Des ...
- poj 1039 Pipe (Geometry)
1039 -- Pipe 理解错题意一个晚上._(:з」∠)_ 题意很容易看懂,就是要求你求出从外面射进一根管子的射线,最远可以射到哪里. 正解的做法是,选择上点和下点各一个,然后对于每个折点位置竖直 ...
- POJ 1039 Pipe 枚举线段相交
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9493 Accepted: 2877 Description ...
- poj 1039 Pipe(叉乘。。。)
题目:http://poj.org/problem?id=1039 题意:有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从 ...
随机推荐
- BZOJ2005: [Noi2010]能量采集(容斥原理 莫比乌斯反演)
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 4727 Solved: 2877[Submit][Status][Discuss] Descript ...
- 用servlet设计OA管理系统时遇到问题
如果不加单引号会使得除变量和int类型的值不能传递 转发和重定向的区别 转发需要填写完整路径,重定向只需要写相对路径.原因是重定向是一次请求之内已经定位到了服务器端,转发则需要两次请求每次都需要完整的 ...
- jQuery-laye插件实现 弹框编辑,异步验证,form验证提交
代码中用到了 jQuery的ajax异步处理,each()循环,submit()页面验证提交form表单,prepend()追加标签,laye插件的弹框效果(如有其它弹框效果可参考官网:http:// ...
- GMT 时间格式转换到 TDateTime (Delphi)
//GMT 时间格式转换到 TDateTime //忽略时区 function GMT2DateTime(const pSour:PAnsiChar):TDateTime; function GetM ...
- [洛谷1156]垃圾陷阱(DP)
[Luogu1156] f[i]表示高度为i时的存活时间 Code #include <cstdio> #include <algorithm> #define N 110 u ...
- 20145202马超 《Java程序设计》第八周学习总结
第十四章 NIO与NIO2 NIO使用频道(channel)来衔接数据节点,对数据区的标记提供了clear(),rewind(),flip(),compact()等高级操作. 想要取得channel的 ...
- 4,版本控制git --忽略特殊文件
有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们,比如保存了数据库密码的配置文件啦,等等,每次git status都会显示Untracked files ...,有强迫症的童鞋心里肯定 ...
- node-sass安装不成功的问题
SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass 简单粗暴的执行上述的命令.
- Android 做项目总结
1.base 2.跳转可以用uihelper 3.activity和处理逻辑分开 4.userhelper保存管理用户登录信息 5.验证输入框的时候,接受的参数不要是String ,而是Edittex ...
- oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图
多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所在的部门 ...