您的位置:首页 > 其它

Pipe--计算几何叉积的应用

2016-08-12 14:42 375 查看
Pipe

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10307 Accepted: 3191
Description

The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem of determining how far the light can reach inside each component of the pipe.
Note that the material which the pipe is made from is not transparent and not light reflecting. 



Each pipe component consists of many straight pipes connected tightly together. For the programming purposes, the company developed the description of each component as a sequence of points [x1; y1], [x2; y2], . . ., [xn; yn], where x1 < x2 < . . . xn . These
are the upper points of the pipe contour. The bottom points of the pipe contour consist of points with y-coordinate decreased by 1. To each upper point [xi; yi] there is a corresponding bottom point [xi; (yi)-1] (see picture above). The company wants to find,
for each pipe component, the point with maximal x-coordinate that the light will reach. The light is emitted by a segment source with endpoints [x1; (y1)-1] and [x1; y1] (endpoints are emitting light too). Assume that the light is not bent at the pipe bent
points and the bent points do not stop the light beam.
Input

The input file contains several blocks each describing one pipe component. Each block starts with the number of bent points 2 <= n <= 20 on separate line. Each of the next n lines contains a pair of real values xi, yi separated by space. The last block is denoted
with n = 0.
Output

The output file contains lines corresponding to blocks in input file. To each block in the input file there is one line in the output file. Each such line contains either a real value, written with precision of two decimal places, or the message Through all
the pipe.. The real value is the desired maximal x-coordinate of the point where the light can reach from the source for corresponding pipe component. If this value equals to xn, then the message Through all the pipe. will appear in the output file.
Sample Input
4
0 1
2 2
4 1
6 4
6
0 1
2 -0.6
5 -4.45
7 -5.57
12 -10.8
17 -16.55
0

Sample Output
4.67
Through all the pipe.


题目链接:http://poj.org/problem?id=1039

第一次感受到了黑书的强大,明知道是个计算几何,明知道是个叉积的应用,但是还是不会做,我是不是太渣了,不过幸亏我也把题解看懂了,看了题解之后这个题根本就不难好吗!!!这两天一直在攻黑书的计算几何部分,从这个题可以看出还是有效果的,继续加油吧。QAQ~

题目的意思是说有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入口处的(x1,y1),(x1,y1-1)之间射入,向四面八方传播,求解光线最远能传播到哪里(取x坐标)或者是否能穿透整个管道.

思路很简单,暴力,暴力每两个点,他让你求一条直线最远能射到哪里,但是上下顶点对限制光线非常关键,首先,我们想到一根光线自始至终未擦到任何顶点,肯定不是最优的(可以通过平移使之优化)。然后,如果只碰到一个顶点,那也不是最优的,可以通过旋转,使它碰到另一个顶点,并且更优。最后要说明,最优光线必然是擦到一个上顶点和一个下顶点,以上三步用反证法证明并不困难,所以留给读者。

于是有了一个简单的算法,任取一个上顶点和一个下顶点(这不就是纯暴力吗!)形成直线l,然后就没有然后了,黑书359页,我把重点和思路都打在这里了,剩下的就是代码了。

我做计算几何的题,不看着模板根本做不下去= = 惭愧。。。学了这么多天,现在还没搞懂模板上的求交点的方法,这让我很尴尬啊。。。迟点请教菊苣们,现在先贴题。利用叉积和定比分点求交点,搞的我很迷啊!

最后,建议好好看看黑书(《算法艺术与信息学竞赛》)的叉积这一章,对于计算几何的帮助实在是太大了。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define eps 1e-7//控制精度
#define inf 0x3f3f3f3f//设置最大值
using namespace std;
const double precision=1e-6;//黑书上的控制精度
struct Point
{
double x;
double y;
}up[100000],under[100000];//上下顶点坐标
int dblcmp(double d)//黑书上的小技巧,返回整数值,便于计算和判断
{
if(fabs(d)<precision)
return 0;
return (d>0)?1:-1;
}
double det(double x1,double y1,double x2,double y2)//计算叉积
{
return x1*y2-x2*y1;//叉积公式
}
double cross(Point a , Point b , Point c)//哪些点需要计算
{
return det(b.x-a.x , b.y-a.y , c.x-a.x , c.y-a.y);
}
bool check(Point A,Point B,Point C,Point D)//看的网上大牛的优化
{
return (dblcmp(cross(A,B,C))*dblcmp(cross(A,B,D))<=0);//一会贴上大牛的链接
}
double fax(Point A,Point B,Point C,Point D)//判断交点
{
double Area1=cross(A,B,C);//就是这个地方,利用叉积和定比分点求交点搞得我很焦灼啊
double Area2=cross(A,B,D);
int c=dblcmp(Area1);
int d=dblcmp(Area2);//这些都是黑书上的板子
if(c*d<0)//规范相交
return (Area2*C.x-Area1*D.x)/(Area2-Area1);
if(c*d==0)//CD的一个顶点在AB上,不规范相交
{
if(c==0)//C在AB上
return C.x;
else//D在AB上
return D.x;
}
return -inf;//无交点
}
int main()
{
int n,i;
while(~scanf("%d",&n)&&n)
{
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&up[i].x,&up[i].y);//输入坐标
under[i].x=up[i].x;
under[i].y=up[i].y-1;
}
bool flag=false;//判断是否贯穿
int j,k;
double maxc=-inf;
for(i=1;i<=n;i++)//开始暴力
{
for(j=1;j<=n;j++)
{
if(i==j)
continue;
for(k=1;k<=n;k++)//直线最大延伸到第k-1条管子
{
if(!check(up[i] , under[j] , up[k] , under[k]))//仔细想想这里就会明白这里是正确的了
break;
}
if(k>n)//已贯穿
{
flag=true;
break;
}
else if(k>max(i,j))//判断是上顶点还是下顶点
{//举例:若直线与上顶点相交,计算下顶点时,得到的是第k-1个下顶点(下折点),并不一定是最优。
double temp=fax(up[i] , under[j] , up[k] , up[k-1]);//直线与上顶点相交
if(maxc<temp)
maxc=temp;
temp=fax(up[i] , under[j] , under[k] , under[k-1]);//直线与下顶点相交
if(maxc<temp)
maxc=temp;
}
}
if(flag)
break;
}
if(flag)
printf("Through all the pipe.\n");//贯穿
else
printf("%.2f\n",maxc);
}
return 0;
}
</span></span>

哎,越来越感觉到智商的重要性,也越来越感觉到我自身知识的不足了,哎,贴上大神的链接

http://blog.csdn.net/lyy289065406/article/details/6648585

必须的大神啊,我还要继续努力!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: