链接:http://poj.org/problem?id=1039
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9513 | Accepted: 2887 |
Description
Input
Output
Sample Input
4 0 1 2 2 4 1 6 4 6 0 1 2 -0.6 5 -4.45 7 -5.57 12 -10.8 17 -16.55 0
Sample Output
4.67 Through all the pipe.
Source
总结下线段和直线的关系之类的
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include<queue> #include<set> #include<map> #include<stack> #include<cmath> #define mst(ss,b) memset((ss),(b),sizeof(ss)) #define maxn 0x3f3f3f3f using namespace std; int n; struct point { double x,y; point(double x=0,double y=0):x(x),y(y) {} } s[22],p[22]; const double eps=1e-8; typedef point vec; vec operator - (point a,point b) { return vec(a.x-b.x,a.y-b.y); } vec operator + (point a,point b) { return vec(a.x+b.x,a.y+b.y); } vec operator * (point a,double t) { return vec(a.x*t,a.y*t); } int dcmp(double x) { if(fabs(x)<=eps) return 0; else return x<0?-1:1; } double cross(vec a,vec b) { ///叉积 当前向量逆时针180度为正,顺时针180度为负 return a.x*b.y-a.y*b.x; ///叉积 绝对值也是有向面积 } bool pd(point a,point b,point c,point d) { double c1=cross(b-a,c-a); double c2=cross(b-a,d-a); /// c1*c2<=0 表示cd在ab左右,并不能表示ab在cd左右 double c3=cross(d-c,a-c); double c4=cross(d-c,b-c); /// c3*c4<=0 表示ab在cd左右,并不能表示cd在ab左右 return dcmp(c1)*dcmp(c2)<=0; /// cd在ab左右才返回true; } double intersection(point a,point b,point c,point d){ double c1=cross(b-a,c-a); double c2=cross(b-a,d-a); if(dcmp(c1)*dcmp(c2)<0){ /// x=(c2*c.x-c1*d.x)/(c2-c1); /// y=(c2*c.y-c1*d.y)/(c2-c1); 求直线ab与线段cd的交点坐标(前提是cd在ab的左右) return (c2*c.x-c1*d.x)/(c2-c1); } if(dcmp(c1)*dcmp(c2)==0){ if(dcmp(c1)==0) return c.x; else return d.x; } return -maxn*1.0; } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; for(int i=0; i<n; i++) { scanf("%lf%lf",&s[i].x,&s[i].y); p[i].x=s[i].x; p[i].y=s[i].y-1; } double ans=-maxn*1.0; int flag=0,k; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(i!=j) { for(k=0; k<n; k++) { if(!pd(s[i],p[j],s[k],p[k])) { break; } } if(k>=n) { flag=1; break; } else if(k>=max(i,j)){ double temp=intersection(s[i],p[j],s[k],s[k-1]); ans=max(temp,ans); temp=intersection(s[i],p[j],p[k],p[k-1]); ans=max(ans,temp); } } } if(flag) break; } if(flag) printf("Through all the pipe.\n"); else printf("%.2f\n",ans); } return 0; }