Line of Sight - POJ 2074 直线交点

Line of Sight
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3878 Accepted: 1211

Description

An architect is very proud of his new home and wants to be sure it can be seen by people passing by his property line along the street. The property contains various trees, shrubs, hedges, and other obstructions that may block the view. For the purpose of this problem, model the house, property line, and obstructions as straight lines parallel to the x axis: 

To satisfy the architect's need to know how visible the house is, you must write a program that accepts as input the locations of the house, property line, and surrounding obstructions and calculates the longest continuous portion of the property line from which the entire house can be seen, with no part blocked by any obstruction.

Input

Because each object is a line, it is represented in the input file with a left and right x coordinate followed by a single y coordinate: 
< x1 > < x2 > < y > 
Where x1, x2, and y are non-negative real numbers. x1 < x2 
An input file can describe the architecture and landscape of multiple houses. For each house, the first line will have the coordinates of the house. The second line will contain the coordinates of the property line. The third line will have a single integer that represents the number of obstructions, and the following lines will have the coordinates of the obstructions, one per line. 
Following the final house, a line "0 0 0" will end the file. 
For each house, the house will be above the property line (house y > property line y). No obstruction will overlap with the house or property line, e.g. if obstacle y = house y, you are guaranteed the entire range obstacle[x1, x2] does not intersect with house[x1, x2].

Output

For each house, your program should print a line containing the length of the longest continuous segment of the property line from which the entire house can be to a precision of 2 decimal places. If there is no section of the property line where the entire house can be seen, print "No View".

Sample Input

2 6 6
0 15 0
3
1 2 1
3 4 1
12 13 1
1 5 5
0 10 0
1
0 15 1
0 0 0

Sample Output

8.80
No View

题意:给你房子的坐标和马路的坐标,然后其中有一些障碍物,问能看到完整房子的最大连续距离是多少。

思路:让房子的左端点和障碍物的右端点连线到马路上,房子的右端点和障碍物的左端点连线到马路上,这一段就是不可取的,排序后处理即可。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
struct node1
{
    double x1,x2;
};
bool cmp(node1 a,node1 b)
{
    return a.x1<b.x1;
}
typedef Point Vector;
double eps=1e-10;
Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double p){return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A,double p){return Vector(A.x/p,A.y/p);}
int dcmp(double x){return (x>eps)-(x<-eps);}
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
void jiaodian(Point aa,Point ab,Point ba,Point bb,Point &as)
{
    double s1=Cross(aa-ba,bb-ba);
    double s2=Cross(ab-ba,bb-ba);
    as=(ab*s1-aa*s2)/(s1-s2);
}
int T,t,n,m;
Point p[2010][2];
node1 arr[2010];
int main()
{
    int i,j,k,pos;
    double st,maxn;
    Point a,b;
    while(~scanf("%lf%lf%lf",&p[1][0].x,&p[1][1].x,&p[1][0].y))
    {
        if(dcmp(p[1][0].x+p[1][1].x+p[1][0].y)==0)
          break;
        p[1][1].y=p[1][0].y;
        scanf("%lf%lf%lf",&p[2][0].x,&p[2][1].x,&p[2][0].y);
        p[2][1].y=p[2][0].y;
        scanf("%d",&n);
        n+=2;
        for(i=3;i<=n;i++)
        {
            scanf("%lf%lf%lf",&p[i][0].x,&p[i][1].x,&p[i][0].y);
            p[i][1].y=p[i][0].y;
            if(dcmp(p[1][0].y-p[i][0].y)<=0 || dcmp(p[2][0].y-p[i][0].y)>=0)
            {
                n--;
                i--;
            }
        }
        m=0;
        for(i=3;i<=n;i++)
        {
            jiaodian(p[1][0],p[i][1],p[2][0],p[2][1],a);
            jiaodian(p[1][1],p[i][0],p[2][0],p[2][1],b);
            m++;
            arr[m].x1=b.x;
            arr[m].x2=a.x;
        }
        arr[++m].x1=p[2][0].x;
        arr[m].x2=p[2][0].x;
        arr[++m].x1=p[2][1].x;
        arr[m].x2=p[2][1].x;
        sort(arr+1,arr+1+m,cmp);
        maxn=0;st=p[2][0].x;
        pos=1;
        while(pos<=m)
        {
            while(pos<=m && dcmp(arr[pos].x1-st)<=0)
            {
                st=max(st,arr[pos].x2);
                pos++;
            }
            if(pos<=m)
              maxn=max(maxn,arr[pos].x1-st);
            st=arr[pos].x2;
            pos++;
        }
        if(dcmp(maxn)==0)
          printf("No View\n");
        else
          printf("%.2f\n",maxn);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值