您的位置:首页 > 其它

POJ 2074(视线与障碍物+求直线和线段交点)

2017-11-23 22:46 525 查看
Line of Sight

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 4496 Accepted: 1393

Description

An architect is very proud of his new home and wants to be sure it can be seen by peopl
4000
e 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

Source

Mid-Atlantic 2004

题意:房子,障碍物,路都是直线,给出N个障碍物,求出连续的在路上能完整看到房子的长度

做法:对每个障碍物都可以投影到路上,这段投影B1A1是看不到完整的房子的,这些区间的并集里的空隙就是能完整看见房子的地方,求出最大值就可以了



这题的坑点在于

1、投影区间端点可能在路外面

2、障碍物的y可能不在房子和路之间

3、障碍物的y=房子或者路的y时,忽略掉

4、用C++交才能过(坑的我啊)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#define maxn 10010
#define maxe 100010
typedef long long ll;
using namespace std;
const double eps=1e-5;
const int inf=0x3f3f3f3f3f;
typedef double T1;
struct Point
{
T1 x,y;
Point(){};
Point(T1 a,T1 b)
{
x=a,y=b;
}
void input()
{
scanf("lf",&x,&y);
}
Point operator +(Point a)
{
Point b(x+a.x,y+a.y);
return b;
}
Point operator -(Point a)
{
Point b(x-a.x,y-a.y);
return b;
}
T1 operator *(Point a)
{
return x*a.x+y*a.y;
}
T1 operator ^(Point a)
{
return x*a.y-y*a.x;
}
bool operator <(Point a)
{
return x<a.x;
}
};
struct Line
{
Point a,b;
double k,d;
bool flag;
void prepare()
{
if(a.x-b.x!=0)
k=(a.y-b.y)*1.0/(a.x-b.x),flag=true,d=a.y-k*a.x;
else flag=false,d=a.x;
}
Line(Point st,Point ed)
{
a=st;
b=ed;
prepare();

}
Line(){};
void input()
{
a.input();
b.input();
prepare();
}
int operator *(Line l)
{
return (a-b)*(l.a-l.b);
}
int operator ^( Line l)
{
return (a-b)^(l.a-l.b);
}
Point intersect(Line l)
{
Point r;
double x,y;
if(!flag&&l.flag)
{
x=d;
y=l.k*x+l.d;
}
else if(flag&&!l.flag)
{
x=l.d;
y=k*x+d;
}
else
{
x=(l.d-d)/(k-l.k);
y=k*x+d;
}
r.x=x;
r.y=y;
return r;

}
}l[maxn],h,pro;
struct Node
{
double a,b;

Node(){};
Node(double x,double y)
{
a=x,b=y;
}
}node[maxn],node1[maxn];
bool cmp(const Node &x,const Node &y)
{
return x.a<y.a;
}
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
return 1;
}
int main()
{
int n;
double x1,x2,y;
Point st,ed;
//freopen("in.txt","r",stdin);
while(scanf("%lf%lf%lf",&x1,&x2,&y)==3)
{
if(x1==0&&x2==0&&y==0)break;
st=Point(x1,y);
ed=Point(x2,y);
h=Line(st,ed);
scanf("%lf%lf%lf",&x1,&x2,&y);
st=Point(x1,y);
ed=Point(x2,y);
pro=Line(st,ed);
scanf("%d",&n);
for(int i=0;i<n;i++)
{ //cout<<"ds"<<endl;
scanf("%lf%lf%lf",&x1,&x2,&y);
st=Point(x1,y);
ed=Point(x2,y);
l[i]=Line(st,ed);
}
Line r;
Point p1,p2;
int flag=0;
int cnt=0;
double maxy=max(pro.a.y,h.a.y);
double miny=min(pro.a.y,h.a.y);
for(int i=0;i<n;i++)
{
if(l[i].a.y<=miny||l[i].a.y>=maxy)continue;
if(l[i].a.y==h.a.y)
{
if(l[i].a.x<h.a.x||l[i].b.x>h.b.x)
{
continue;
}
if((l[i].a.x>h.a.x&&l[i].a.x<h.b.x)||(l[i].b.x<h.b.x&&l[i].b.x>h.a.x))
{
flag=1;break;
}
}
r=Line(h.a,l[i].b);
p1=r.intersect(pro);
r=Line(h.b,l[i].a);
p2=r.intersect(pro);
if(p1.x>=pro.b.x)
p1=pro.b;
else if(p1.x<=pro.a.x)
p1=pro.a;
if(p2.x<=pro.a.x)
p2=pro.a;
else if(p2.x>=pro.b.x)
p2=pro.b;
//            if(l[i].b.x<pro.a.x||l[i].a.x>pro.b.x)
//            {
//                continue;
//            }
node[cnt++]=Node(p2.x,p1.x);
}
if(flag){puts("No View");continue;}
sort(node,node+cnt,cmp);
//        for(int i=0;i<cnt;i++)
//        {
//            printf("%lf %lf\n",node[i].a,node[i].b);
//        }
double st1=node[0].a,ed1=node[0].b;
double sum=0.0;
node[cnt++]=Node(pro.b.x,pro.b.x+2);
double len=node[0].a-pro.a.x;
for(int i=1;i<cnt;i++)
{
if(sgn(node[i].a==node[i].b))continue;
if(sgn(node[i].a-ed1>=0))
{
sum=ed1-st1+sum;
len=max(len,node[i].a-ed1);
st1=node[i].a;
ed1=node[i].b;
}
else if(node[i].b>ed1)
{
ed1=node[i].b;
}
}
double ans=pro.b.x-pro.a.x-sum;
if(sgn(ans)<=0)puts("No View");
else printf("%.2lf\n",len);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: