poj 2074 Line of Sight(计算几何)

来源:互联网 发布:放开那三国2等级数据 编辑:程序博客网 时间:2024/05/20 03:39

Line of Sight
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 3139 Accepted: 987

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 60 15 031 2 13 4 112 13 11 5 50 10 010 15 10 0 0

Sample Output

8.80No View

Source

Mid-Atlantic 2004

题意:给定一个屋子和一条路,还有一些障碍物,所有东西都是平行于x轴的直线,问在路上面可以看见完整的屋子的长度最长是多少

题解:先排除一些不在屋子和路中间的障碍物,然后枚举障碍物,屋子的左端点和障碍物的右端点所连直线和路的交点1,屋子的右端点和障碍物的左端点所连直线和路的交点2,这2个交点间的路就不能看见完整的屋子,然后求所有交点先离散化出来,就把路分成很多段,而且没可能2段连续都能看见完整的屋子的(因为1段路肯定是某部分不能看见,所以才被分割成2段)所以只需再枚举一次线段,把不能看见的都标记起来,然后再遍历一次看最大值就可以了


#include<stdio.h>#include<string.h>#include<algorithm>#define eps 1e-8using namespace std;struct point{    double x,y;    point(){}    point(double x_,double y_):x(x_),y(y_){}};struct position{    double x1,x2,y;}house,line,ob[100008],res[100008];double a,b,c,l,r,ans,p[100008];int flag[100008];int zero(double x){ return -eps<x&&x<eps; }void get_line(){    a=line.y-line.y;    b=line.x2-line.x1;    c=line.x1*line.y-line.y*line.x2;}double get_intersect(struct point p1,struct point p2){    double a2=p1.y-p2.y;    double b2=p2.x-p1.x;    double c2=p1.x*p2.y-p1.y*p2.x;    double tmd=a*b2-a2*b;    return (b*c2-b2*c)/tmd;}int main(){    int i,j,n,all,alln;    while(scanf("%lf%lf%lf",&house.x1,&house.x2,&house.y)>0)    {        if(!house.x1&&!house.x2&&!house.y) break;        scanf("%lf%lf%lf",&line.x1,&line.x2,&line.y);        scanf("%d",&n);        for(i=0;i<n;i++) scanf("%lf%lf%lf",&ob[i].x1,&ob[i].x2,&ob[i].y);        get_line();        for(all=alln=i=0;i<n;i++)        {            if(ob[i].y-house.y>-eps) continue;            if(line.y-ob[i].y>eps) continue;            res[all].x1=get_intersect(point(ob[i].x1,ob[i].y),point(house.x2,house.y));            res[all].x2=get_intersect(point(ob[i].x2,ob[i].y),point(house.x1,house.y));            if(res[all].x1>res[all].x2) swap(res[all].x1,res[all].x2);            if(res[all].x1<line.x1) res[all].x1=line.x1;            if(res[all].x1>line.x2) res[all].x1=line.x2;            if(res[all].x2<line.x1) res[all].x2=line.x1;            if(res[all].x2>line.x2) res[all].x2=line.x2;            p[alln++]=res[all].x1;            p[alln++]=res[all].x2;            all++;        }        p[alln++]=line.x1,p[alln++]=line.x2;        sort(p,p+alln);        alln=unique(p,p+alln)-p;        memset(flag,0,sizeof(flag));        for(i=0;i<all;i++)        {            l=lower_bound(p,p+alln,res[i].x1)-p;            r=lower_bound(p,p+alln,res[i].x2)-p;            for(j=l;j<r;j++) flag[j]=1;        }        for(ans=i=0;i<alln-1;i++)        {            if(flag[i]) continue;            if(p[i+1]-p[i]>ans) ans=p[i+1]-p[i];        }        if(zero(ans)) printf("No View\n");        else printf("%.2f\n",ans);    }    return 0;}


0 0