http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2325
本题可以直接排序再算可以看到全部的那段线段长度,也可以用相似三角形来做,我选择后者
具体方法:先把障碍物按x从小到大排序,然后就遍历,令top=L.x1,记得要先连接a[i].x1与hou.x2,再用相似三角形求出要求区域的右端点,在类似的求出区域的左端点,要注意不能超过L.x2,因为最后还要L.x2-top,至于L.x1就不用讨论,不影响结果,具体看代码
#include<iostream>
#include<vector>
#include<map>
#include<stack>
#include<algorithm>
#include<queue>
#include<list>
#include<set>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<iomanip>
using namespace std;
#define LL long long
#define pi acos(-1)
#define N 50010
#define INF 9999999999
#define eps 1e-8
struct point
{
double x1,x2,y;
}a[1000],hou,L;
double f(double x,double h1,double h2)
{
return x*(h1/h2);
}
bool cmp(point s,point t)
{
return s.x1<t.x1;
}
int main()
{
// freopen("a.txt","r",stdin);
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",&L.x1,&L.x2,&L.y);
int n;
scanf("%d",&n);
int i,j;
for(i=0;i<n;i++)
scanf("%lf%lf%lf",&a[i].x1,&a[i].x2,&a[i].y);
sort(a,a+n,cmp);
double top=L.x1;
double temp;
double ans=0;
for(i=0;i<n;i++)
{
if(a[i].y>=hou.y||a[i].y<L.y)continue;
temp=a[i].x1-f( hou.x2-a[i].x1, a[i].y-L.y, hou.y-a[i].y );
if(temp>=L.x2)break;
if(temp>top)
ans=ans<temp-top ? temp-top :ans;
temp=a[i].x2+f( a[i].x2-hou.x1, a[i].y-L.y, hou.y-a[i].y );
if(temp>top)top=temp;
if(top>=L.x2)break;
}
if(top<L.x2)
ans=ans<L.x2-top ?L.x2-top :ans;
if(ans>0)
printf("%.2f\n",ans);
else
printf("No View\n");
}
return 0;
}