实践告诉我们有小数的时候选择C++ G++90%会挂
#include <iostream>
#include <string.h>#include <stdio.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
#define eps 1e-8
struct Point
{
double x, y;
Point(){};
Point (double xx, double yy)
{
x = xx;
y = yy;
}
}up[33], down[33];
double Across( Point a, Point b, Point c)
{
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y)*(c.x - a.x);
}
int sig(double a)
{
if(a < -eps)
return -1;
if(a > eps)
return 1;
return 0;
}
bool check1(Point a, Point b, Point c, Point d)//是否可以通过管口
{
int d1 = sig(Across(a, b, c));
int d2 = sig(Across(a, b, d));
if(d1*d2 <= 0)
return true;
return false;
}
bool check2(Point a, Point b, Point c, Point d)//是否交点在管壁
{
int d1 = sig(Across(a, b, c));
int d2 = sig(Across(a, b, d));
if(d1*d2 < 0)
return true;
return false;
}
Point GetPoint(Point a, Point b, Point c, Point d)
{
Point temp=a;
double t=((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))
/((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));
temp.x+=(b.x-a.x)*t;
temp.y+=(b.y-a.y)*t;
return temp;
}
void solve( int n )//判断能否进入一个管口时,直线与线段的相交要包括端点,而判断直线与管壁是否相交时,不能包括端点。
{
double ansx = up[0].x;
Point res;
for( int i = 0; i < n; i++)
{
for( int j = 0; j < n; j++)
{
if(!check1(up[i], down[j], up[0], down[0]))
continue;
int k;
for( k = 1; k < n; k++)
{
//判断是否能经过下一个管口
//不能的话交点一定在k-1, k 之间
if(!check1(up[i], down[j], up[k], down[k]))
{
if(check2(up[i], down[j], up[k-1], up[k]))
{
res = GetPoint(up[i], down[j], up[k-1], up[k]);
ansx = max(res.x , ansx);
break;
}
if(check2(up[i], down[j], down[k-1], down[k]))
{
res = GetPoint(up[i], down[j], down[k-1], down[k]);
ansx = max(res.x , ansx);
break;
}
ansx = max(ansx, up[k-1].x);
break;
}
}
if(k == n)
{
printf("Through all the pipe.\n");
return ;
}
}
}
printf("%.2lf\n",ansx);
}
int main()
{
int n;
while(scanf("%d",&n) != EOF && n)
{
for(int i = 0; i < n; i++)
{
scanf("%lf %lf",&up[i].x, &up[i].y);
down[i] = Point(up[i].x, up[i].y-1);
}
solve(n);
}
}