您的位置:首页 > 其它

POJ 2074 Line of Sight

2013-08-19 20:18 344 查看
POJ 2074 Line of Sight

线段相交

其实就是枚举障碍物的坐端点和房子的右端点 还有就是障碍的右端点和房子的左端点构成的直线和风景的交点对;

然后就是求这中间没有被覆盖的最长的长度。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define DOR(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)

#define oo 1e6
#define eps 1e-8
#define nMax 100000

#define mp make_pair
#define pb push_back
#define F first
#define S second

#define bug puts("OOOOh.....");
#define zero(x) (((x)>0?(x):-(x))<eps)

int dcmp(double x){
if(fabs(x)<eps) return 0;
return x>0?1:-1;
}
class point {
public:
double x,y;
point (double x=0,double y=0):x(x),y(y) {}
void make(double _x,double _y) {x=_x;y=_y;}
void read() { scanf("%lf%lf",&x,&y); }
void out() { printf("%.2lf %.2lf\n",x,y);}
double len() { return sqrt(x*x+y*y); }
point friend operator - (point const& u,point const& v) {
return point(u.x-v.x,u.y-v.y);
}
point friend operator + (point const& u,point const& v) {
return point(u.x+v.x,u.y+v.y);
}
double friend operator * (point const& u,point const& v) {
return u.x*v.y-u.y*v.x;
}
double friend operator ^ (point const& u,point const& v) {
return u.x*v.x+u.y*v.y;
}
point friend operator * (point const& u,double const& k) {
return point(u.x*k,u.y*k);
}
};
typedef class line{
public:
point a,b;
line() {}
line (point a,point b):a(a),b(b){}
void make(point u,point v) {a=u;b=v;}
void make(double x1,double x2,double y){make(point(x1,y),point(x2,y));}
void read() { scanf("%lf%lf%lf",&a.x,&b.x,&a.y);b.y=a.y; }
friend double intersection(line u,line v);
}segment;
double intersection(line u,line v){
point ret=u.a;
double t=(u.a-v.a)*(v.a-v.b)/((u.a-u.b)*(v.a-v.b));
ret = ret + (u.b-u.a)*t;
return ret.x;
}

line H,P,s[nMax];
int n;
vector<pair<double,double> > V;

void sovle(){
V.clear();
for(int i=1;i<=n;i++) {
if(dcmp(H.a.y-s[i].a.y)>0 && dcmp(s[i].a.y-P.a.y)>0){
V.pb(mp(intersection(line(H.b,s[i].a),P),intersection(line(H.a,s[i].b),P)));
}
}
V.pb(mp(P.b.x,P.b.x));
sort(V.begin(),V.end());
double ans=0;
double nb=P.a.x;
for(int i=0;i<V.size();i++) {
if(dcmp(V[i].F-nb)>0) {
ans = max(ans,V[i].F-nb);
}
if(dcmp(V[i].S-nb)>0){
nb=V[i].S;
}
}
if(dcmp(ans)==0) printf("No View\n");
else printf("%.2lf\n",ans + eps);
return ;
}

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
double x1,x2,y;
while(~scanf("%lf%lf%lf",&x1,&x2,&y),dcmp(x1)||dcmp(x2)||dcmp(y)){
H.make(x1,x2,y);
P.read();
scanf("%d",&n);
FOR(i,1,n) s[i].read();
sovle();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: