题目链接:https://vjudge.net/problem/POJ-2826#author=0
题意:给你两个线段板子,用这个接雨水,问最多能接多少水。
思路:wa了13发终于过了,少考虑了一种情况一直wa。首先如果有一块板子是水平的,那肯定接不了雨水;其次,如果二块板子没有交点,那接的雨水都会漏掉;最后如果两块板子有交点且那个交点不是某块板子的最高点,然后要特判一种特殊情况,它是接不到雨水的,特判图形如下。然后接的水的高度是较低的那块板子。这些找线段的交点和一些判断什么的都可以用差积解决。
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<queue> using namespace std; typedef long long ll; #define INF 0x7ffffff double chaj(double x1,double y1,double x2,double y2) { return x1*y2-x2*y1; } int main() { int t; cin>>t; while(t--) { double x1,y1,x2,y2; double x3,y3,x4,y4; cin>>x1>>y1>>x2>>y2; cin>>x3>>y3>>x4>>y4; if(y1==y2||y3==y4) { cout<<"0.00"<<endl; continue; } if(chaj(x3-x1,y3-y1,x2-x1,y2-y1)==0&&chaj(x4-x1,y4-y1,x2-x1,y2-y1)==0) { cout<<"0.00"<<endl; continue; } if(chaj(x3-x1,y3-y1,x2-x1,y2-y1)*chaj(x4-x1,y4-y1,x2-x1,y2-y1)<=0&&chaj(x1-x3,y1-y3,x4-x3,y4-y3)*chaj(x2-x3,y2-y3,x4-x3,y4-y3)<=0) { double s1=fabs(chaj(x3-x1,y3-y1,x4-x1,y4-y1)); double s2=fabs(chaj(x3-x2,y3-y2,x4-x2,y4-y2)); double w=s1/(s1+s2); double x=w*(x2-x1)+x1,y=w*(y2-y1)+y1; double x5,y5,x6,y6; if(y1>y2) { x5=x1; y5=y1; } else { x5=x2; y5=y2; } if(y3>y4) { x6=x3; y6=y3; } else { x6=x4; y6=y4; } if(y5<=y||y6<=y) { cout<<"0.00"<<endl; continue; } x5-=x;y5-=y; x6-=x;y6-=y; if(x5<0&&x6<0) { if(y5<y6&&x5>=x6&&chaj(x5,y5,x6,y6)<0) { cout<<"0.00"<<endl; continue; } if(y6<y5&&x6>=x5&&chaj(x6,y6,x5,y5)<0) { cout<<"0.00"<<endl; continue; } } if(x5>0&&x6>0) { if(y5<y6&&x5<=x6&&chaj(x5,y5,x6,y6)>0) { cout<<"0.00"<<endl; continue; } if(y5>y6&&x5>=x6&&chaj(x6,y6,x5,y5)>0) { cout<<"0.00"<<endl; continue; } } if(y5>y6) { x5=y6*x5/y5; y5=y6; } else if(y5<y6) { x6=y5*x6/y6; y6=y5; } double ans=fabs(chaj(x5,y5,x6,y6)); printf("%.2lf ",ans/2); } else cout<<"0.00"<<endl; } }