poj2074 Line of Sight【计算几何】

来源:互联网 发布:单位网络监控 编辑:程序博客网 时间:2024/06/11 16:54

题目大意:

题意:给出一个房子(看成线段)的端点坐标,和一条观光区的两端坐标,给出一些障碍物(看成线段)的两端坐标(都平行于x轴)。问在路上能看到完整房子的最大连续长度是多长。
【注意】
1. 输入的线段(x1,x2,y)保证x1<x2
2. 输入的home一定在观光区上面
3. 输入的障碍物不一定在home和观光区之间
4. 如果输入的障碍物与home同y轴,不管重合与部分重合还是不重合,不算遮住。
5. 如果输入的障碍物与观光区重合,同理。

解题思路:

求出每个障碍在观光区上所阻挡的视野区间,然后去掉被覆盖的小区间求相邻两个区间之间的间隙最大值即可。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>#include<ctime>#include<vector>#include<queue>#define ll long longusing namespace std;int getint(){    int i=0,f=1;char c;    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());    if(c=='-')c=getchar(),f=-1;    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';    return i*f;}const int N=1005;const double eps=1e-8;int n;double ans;struct node{    double x1,x2,y;    inline friend bool operator <(const node &a,const node &b)    {        if(a.x1==b.x1)return a.x2>b.x2;        return a.x1<b.x1;    }}a[N],h,p;double calc(double x1,double y,double x2){    if(fabs(x1-x2)<eps)return x1;    double k=(h.y-y)*1.0/(x2-x1);    double m=y-k*x1;    double res=(p.y-m)*1.0/k;    return res;}int main(){    //freopen("lx.in","r",stdin);    double x1,x2,y;    while(scanf("%lf%lf%lf",&h.x1,&h.x2,&h.y))    {        if(!h.x1&&!h.x2&&!h.y)break;        ans=0;        scanf("%lf%lf%lf",&p.x1,&p.x2,&p.y);        n=getint();        for(int i=1;i<=n;i++)        {            scanf("%lf%lf%lf",&x1,&x2,&y);            if(y>=h.y||y<=p.y)            {                i--,n--;                continue;            }            a[i].x1=calc(x1,y,h.x2);            a[i].x2=calc(x2,y,h.x1);        }        ++n,a[n].x1=a[n].x2=p.x1;        ++n,a[n].x1=a[n].x2=p.x2;        sort(a+1,a+n+1);        int top=1;        for(int i=2;i<=n;i++)            if(a[i].x2>a[top].x2)a[++top]=a[i];        n=top;        int i=1;        while(a[i].x2<p.x1)i++;        for(i;i<n;i++)ans=max(ans,a[i+1].x1-a[i].x2);        if(!ans)puts("No View");        else printf("%0.2f\n",ans);    }    return 0;}
原创粉丝点击