1. 程式人生 > >Line of Sight POJ

Line of Sight POJ

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

題意:求在公路上看到房子,求能看到房子的最大連續的公路長度是多少?

思路:反過來想,先求不能看到房子的公路盲區,即求房子左端點與障礙物右端點作直線,該直線與公路的交點作為該障礙物造成的盲區的右端點,房子右端點與障礙物左端點作直線,該直線與公路的交點作為該障礙物造成的盲區的左端點,然後對盲區按左端點從小到大的順序排序,最後取所有當前盲區的最左端點與前面盲區延伸到最有右端的差值(連續非盲區)的最大值,要注意判斷這個差值要落在公路上。

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1100;
const int nmax = 50010;
const double esp = 1e-8;
const double PI=3.1415926;
int n;
double hx1,hx2,hy,px1,px2,py;
struct block
{
    double x1,x2;
    bool operator <(const block &b)const
    {
        return x1<b.x1;  //盲區按左端點從小到大排序
    }
} b[nmax];//公路盲區結構體
double intera(double hx,double x2,double y2)  
{
    if(fabs(hy-y2)<esp||fabs(x2-hx)<esp)  //障礙物與房子共線或兩點連線垂直x軸,直接得出返回值
        return hx;
    double k=(hy-y2)/(hx-x2);  
    return (hy-k*hx-py)/(-k);
}
int main()
{
    double ox1,ox2,oy;
    while(scanf("%lf%lf%lf",&hx1,&hx2,&hy)!=EOF)
    {
        if(hx1==0&&hx2==0&&hy==0)
            break;
        scanf("%lf%lf%lf",&px1,&px2,&py);
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf%lf",&ox1,&ox2,&oy);
            if(oy>hy||oy<py)   //不在房子和公路之間的障礙物才不可能造成盲區
            {
                b[i].x1=-inf;
                b[i].x2=-inf;
                continue;
            }
             //房子左端點與障礙物右端點作直線,求該直線與公路的交點
            b[i].x2=intera(hx1,ox2,oy); 
             //房子右端點與障礙物左端點作直線,求該直線與公路的交點
            b[i].x1=intera(hx2,ox1,oy);
        }
        if(n==0){
            printf("%.2f\n",px2-px1);
            continue;
        }
        b[n].x1=-inf;
        b[n].x2=px1;
        b[n+1].x1=px2;
        b[n+1].x2=inf;
        sort(b,b+n+2);
        double ri=b[0].x2,maxl=(double)(-inf);  //ri為前面最右邊的盲點
        for(int i=1;i<n+2;i++){
            if(b[i].x1-ri>esp){//當前點左端點大於最右端的盲區點
                  maxl=max(maxl,(b[i].x1<px2?b[i].x1:px2)-(ri>px1?ri:px1));  
            //判斷最右端的盲區點是否大於公路最左邊,判斷當前點左端點是否小於公路最右端,取有效值
            }
            ri=max(ri,b[i].x2);
        }
        if(maxl>esp)
            printf("%.2lf\n",maxl);
      else
            printf("No View\n");
    }
    return 0;
}