poj 2074

直线的交点,题目不是很难,但还是太粗心,其中有一步疏忽,导致无限wa,最后还是发现了

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=10000;
double eps=1e-10;
struct node
{
	double x1,x2,y;
};
struct Point 
{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y) {};
};
node arc[maxn],hou,line;
int n;
struct node2
{
	double l1,l2;
};
node2 ran[maxn];
double max(double a,double b) { return a>b?a:b; }
double cross(double x1,double y1,double x2,double y2)
{
	return x1*y2-y1*x2;
}
double getIn(Point a,Point b)
{
	double vx=b.x-a.x,vy=b.y-a.y;
	double wx=line.x2-line.x1,wy=0;
	double ux=a.x-line.x1,uy=a.y-line.y;
	double t=cross(wx,wy,ux,uy)/cross(vx,vy,wx,wy);
	return a.x+t*vx;
}
bool cmp(node2 a,node2 b)
{
	return a.l1<b.l1;
}
double solve()
{
	int i;
	int tot=0;
	for(i=0;i<n;i++)
	{
		if(!(arc[i].y>hou.y||arc[i].y<line.y))
		{
			double l1=getIn(Point(hou.x1,hou.y),Point(arc[i].x2,arc[i].y)),
				   l2=getIn(Point(hou.x2,hou.y),Point(arc[i].x1,arc[i].y));
			if(l1<line.x1) l1=line.x1;
			if(l1>line.x2) l1=line.x2;
			if(l2<line.x1) l2=line.x1;
			if(l2>line.x2) l2=line.x2;
			ran[tot].l1=l2;
			ran[tot].l2=l1;
			tot++;
		}
	}
	sort(ran,ran+tot,cmp);
	double maxv=0;
	if(tot==0) return line.x2-line.x1;//思维不够严密,居然拉了这步,居然想了4个小时
	if(ran[0].l1>line.x1) maxv=ran[0].l1-line.x1;
	double tem=ran[0].l2;
	for(i=1;i<tot;i++)
	{
		if(ran[i].l1>tem)
		{
			maxv=max(maxv,ran[i].l1-tem);
			tem=ran[i].l2;
		}
		else tem=max(tem,ran[i].l2);
	}
	maxv=max(maxv,line.x2-ran[tot-1].l2);
	return maxv;
}
int main()
{
	while(scanf("%lf%lf%lf",&hou.x1,&hou.x2,&hou.y))
	{
		if(hou.x1==0&&hou.x2==0&&hou.y==0) break;
		scanf("%lf%lf%lf",&line.x1,&line.x2,&line.y);
		int i;
		scanf("%d",&n);
		for(i=0;i<n;i++) scanf("%lf%lf%lf",&arc[i].x1,&arc[i].x2,&arc[i].y);
		double ans=solve();
		if(ans==0) printf("No View\n");
	    else printf("%.2lf\n",ans);
	}
	return 0;
}


posted @ 2013-03-07 18:48  LJ_COME!!!!!  阅读(121)  评论(0编辑  收藏  举报