poj 2074 Line of Sight

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 <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);
//	printf("%lf %lf\n",s[tot].s,s[tot].e);
	s[tot].s=(a.e-b.s)*(p.y-b.y)/(a.y-b.y)+b.s;
	s[tot].e=(a.s-b.e)*(p.y-b.y)/(a.y-b.y)+b.e;
	s[tot].y=p.y;
//	printf("%lf %lf\n\n",s[tot].s,s[tot].e);
	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;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值