题目链接:https://vjudge.net/problem/POJ-2074

坑点:障碍物得在up直线和down直线之间。ve得多加一个down的终点,因为可能全部都能看到。最后用float,不要用double。。。。。我也不知道为什么。。。

 1 /*************************************************************************
 2     > File Name: poj2074.cpp
 3 # File Name: poj2074.cpp
 4 # Author : xiaobuxie
 5 # QQ : 760427180
 6 # Email:[email protected]
 7 # Created Time: 2019年09月22日 星期日 16时06分36秒
 8  ************************************************************************/
 9
10 #include<iostream>
11 #include<cstdio>
12 #include<map>
13 #include<cmath>
14 #include<cstring>
15 #include<set>
16 #include<queue>
17 #include<vector>
18 #include<algorithm>
19 using namespace std;
20 typedef long long ll;
21 #define inf 0x3f3f3f3f
22 #define pq priority_queue<int,vector<int>,greater<int> >
23 ll gcd(ll a,ll b){
24     if(a<b) return gcd(b,a);
25     return b==0?a:gcd(b,a%b);
26 }
27
28 #define eps 1e-10
29 #define pii pair < double,double >
30 const int N=1e5+9;
31 int sgn(double x){
32     if(fabs(x)<eps) return 0;
33     if(x<0) return -1;
34     return 1;
35 }
36 struct Point{
37     double x,y;
38     Point operator - (const Point& b)const{
39         return (Point){x-b.x,y-b.y};
40     }
41     double operator * (const Point& b)const{
42         return x*b.x+y*b.y;
43     }
44     double operator ^ (const Point& b )const{
45         return x*b.y-b.x*y;
46     }
47 };
48 struct Line{
49     Point s,e;
50 }L[N],up,down;
51 double solve(Line l1,Line l2){
52     Point res = l1.s;
53     double t = ( (l1.s-l2.s) ^ (l2.s-l2.e))/((l1.s-l1.e)^(l2.s-l2.e));
54     res.x+=(l1.e.x-l1.s.x)*t;
55     return res.x;
56 }
57 int main(){
58     double x1,x2,y;
59     int n;
60     while(~scanf("%lf %lf %lf",&x1,&x2,&y)){
61         if(sgn(x1)==0 && sgn(x2)==0 && sgn(y)==0) break;
62         vector<pii> ve;
63         up=(Line){(Point){x1,y},(Point){x2,y}};
64         scanf("%lf %lf %lf",&x1,&x2,&y);
65         down=(Line){(Point){x1,y},(Point){x2,y}};
66         scanf("%d",&n);
67         for(int i=1;i<=n;++i){
68             scanf("%lf %lf %lf",&x1,&x2,&y);
69             if(sgn(y-up.s.y)>=0 || sgn(down.s.y-y)>=0) continue;
70             double u=solve((Line){up.e,(Point){x1,y}},down);
71             double v=solve((Line){up.s,(Point){x2,y}},down);
72             if(sgn(u-v)>=0) continue;
73             ve.push_back(make_pair(u,v));
74         }
75         ve.push_back(make_pair(down.e.x,down.e.x));
76         sort(ve.begin(),ve.end());
77         double ans=0,pos=down.s.x;
78         for(int i=0;i<ve.size();++i){
79             x1=ve[i].first,x2=ve[i].second;
80             if(x1-pos>ans) ans=x1-pos;
81             if(sgn(x2-pos)>0){
82                 pos=x2;
83                 if(sgn(pos-down.e.x)>0) break;
84             }
85         }
86         if(sgn(ans)==0) puts("No View");
87         else printf("%.2f\n",ans);
88     }
89     return 0;
90 }
View Code
02-12 00:57