您的位置:首页 > 其它

POJ 2074 Line of Sight(判线段与直线相交)

2012-08-14 23:37 513 查看
题目大意:一栋漂亮的房子希望道路上的人可以看见,但是有一些障碍物在房子周围,无法透过障碍物看到房子。求可以在道路上看到房子的最大连续距离。

有这么两点要注意:障碍可能在房子后,与房子平行,或者在道路的另一侧。这些障碍都不用算的。

障碍可能在道路和房子外。(总之就是障碍可能在anywhere!)

所求的是最大连续距离。

//Memory: 216K
//Time: 0MS
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
int tot;
struct LINESEG
{
double s,e,y;
};LINESEG l[1000],s[1000];
LINESEG h,p;
void getx(LINESEG a,LINESEG b)//计算在道路上看不到的端点
{
double temp;
s[tot].s=((b.s*a.y-a.e*b.y)+p.y*(a.e-b.s))/(a.y-b.y);
s[tot].e=((b.e*a.y-a.s*b.y)+p.y*(a.s-b.e))/(a.y-b.y);
s[tot].y=p.y;
if(s[tot].s>s[tot].e)
{
temp=s[tot].s;
s[tot].s=s[tot].e;
s[tot].e=temp;
}
tot++;
}
bool cmp(LINESEG a,LINESEG b)
{
if(a.s==b.s)
return a.e<b.e;
return a.s<b.s;
}
int main()
{
int i,n;
while(~scanf("%lf%lf%lf",&h.s,&h.e,&h.y))
{
tot=0;
double r=-99999999,maxl=0;
memset(s,0,sizeof(s));
if(h.s==0 && h.e==0 && h.y==0)
break;
scanf("%lf%lf%lf",&p.s,&p.e,&p.y);
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%lf%lf%lf",&l[i].s,&l[i].e,&l[i].y);
if(l[i].y>=h.y || l[i].y<p.y)
continue;
getx(h,l[i]);
}
sort(s,s+tot,cmp);
r=s[0].e;
if(s[0].s>p.s)
{
if(s[0].s<=p.e)
maxl=max(maxl,s[0].s-p.s);
else
maxl=max(maxl,p.e-p.s);
}
for(i=1;i<n;i++)
{
if(s[i].s>r)
{
if(s[i].s>p.e)
maxl=max(maxl,p.e-r);
else
maxl=max(maxl,s[i].s-r);
r=s[i].e;
}
else if(s[i].e>r)
r=s[i].e;
}
if(p.e>r)
maxl=max(maxl,p.e-r);
if(maxl<0.0000001)
printf("No View\n");
else
printf("%.2lf\n",maxl);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ini