Pipe
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 240 Accepted Submission(s): 99
Each pipe component consists of many straight pipes connected tightly together. For the programming purposes, the company developed the description of each component as a sequence of points [x1; y1], [x2; y2], . . ., [xn; yn], where x1 < x2 < . . . xn . These are the upper points of the pipe contour. The bottom points of the pipe contour consist of points with y-coordinate decreased by 1. To each upper point [xi; yi] there is a corresponding bottom point [xi; (yi)-1] (see picture above). The company wants to find, for each pipe component, the point with maximal x-coordinate that the light will reach. The light is emitted by a segment source with endpoints [x1; (y1)-1] and [x1; y1] (endpoints are emitting light too). Assume that the light is not bent at the pipe bent points and the bent points do not stop the light beam.
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
Through all the pipe.
题解:中间找x点坐标还没理解,中间判断相交,以及与上下管道相交处理的很巧妙;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
struct Node{
double x,y;
};
Node point[][];
double chaji(Node a,Node b,Node c){
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double is_intersection(Node a,Node b,Node c,Node d){
return chaji(a,b,c)*chaji(a,b,d);
}
double area(Node a,Node b,Node c){
double ab,bc,ac;
ab=sqrt(1.0*pow(b.x-a.x,)+pow(b.y-a.y,));
ac=sqrt(1.0*pow(c.x-a.x,)+pow(c.y-a.y,));
bc=sqrt(1.0*pow(c.x-b.x,)+pow(c.y-b.y,));
double p=(ab+bc+ac)/2.0;// /2
return sqrt(p*(p-ac)*(p-ab)*(p-bc));
}
double getx(Node a,Node b,Node c,Node d){
double s1=area(a,b,c),s2=area(a,b,d);
return (s1*d.x+s2*c.x)/(s1+s2);//找x坐标,没理解太清;
}
int main(){
int n;
while(scanf("%d",&n),n){
for(int i=;i<n;i++){
scanf("%lf%lf",&point[i][].x,&point[i][].y);
point[i][].x=point[i][].x;
point[i][].y=point[i][].y-;
}
Node s_1=point[][],s_2=point[][],a,b;
double ans=-INF;
int flot=;
for(int q=;q<n;q++){
for(int w=;w<;w++){
a=point[q][w];
for(int e=q+;e<n;e++){
for(int r=;r<;r++){
b=point[e][r];
if(is_intersection(a,b,s_1,s_2)<=){
int t;
for(t=;t<n;t++){
if(is_intersection(a,b,point[t][],point[t][])>){
double x;
if(chaji(a,b,point[t][])>)
x=getx(a,b,point[t-][],point[t][]);
else x=getx(a,b,point[t-][],point[t][]);
ans=max(ans,x);
break;
}
}
if(t==n)flot=;
}
}
}
}
}
if(flot)puts("Through all the pipe.");
else printf("%.2f\n",ans);
}
return ;
}