POJ - 2074 Line of Sight

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lidengdengter/article/details/82144691

题目链接

房子和路之间有诸多障碍物,求从这条路上能看到完整房子的最大长度。

换位思考,利用三角形相似原理根据坐标求出视线盲区的长度,除此之外,预处理掉一些不在房子和路之间的障碍物,以免错误判断。参考题解:https://blog.csdn.net/f_cpp/article/details/40952709

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=105;
const double eps=1e-8;
double hx1,hx2,hy;
double px1,px2,py;
double x1,x2,y;
int n,cnt;

struct node{
	double l,r;
}ob[N];

bool cmp1(node a,node b){
	return a.l<b.l;
}

double cal(double x1,double y1,double x2,double y2){
	return x1-(y1-py)*(x2-x1)/(y2-y1);  //相似三角形比例关系 
}

void work(){
	cnt=0;
	for(int i=1;i<=n;i++){
		scanf("%lf%lf%lf",&x1,&x2,&y);
		if(y>=hy||y<=py)
			continue;
		double st,ed;
		st=cal(x1,y,hx2,hy);
		ed=cal(x2,y,hx1,hy);
		if(ed-px1<=eps||st-px2>=-eps)
			continue;
		cnt++;
		ob[cnt].l=max(st,px1);ob[cnt].r=min(ed,px2);
	}
	sort(ob+1,ob+1+cnt,cmp1);	
}

int main(){
	while(~scanf("%lf%lf%lf",&hx1,&hx2,&hy)){
		if(hx1==0&&hx2==0&&hy==0)
			break;
		scanf("%lf%lf%lf",&px1,&px2,&py);
		scanf("%d",&n);
		work();

		double len=0,last=0;
		for(int i=1;i<=cnt;i++){
			if(ob[i].l>last){
				len=max(len,ob[i].l-last);
				last=ob[i].r;
			}
			else
				last=max(last,ob[i].r);
		}
		len=max(len,px2-last); 
		if(fabs(len)<eps)
			printf("No View\n");
		else
			printf("%.2lf\n",len);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/lidengdengter/article/details/82144691