POJ_1039
比较容易理解,如果光线最优的话是可以认为至少与某两个点相切的,因为如果不和某两个点相切,我们可以将光线进行平移和旋转使其至少和某两个点相切,而且结果不会变差。
于是我们可以枚举任意两个顶点确定一条直线作为光线的路径,之后只要看这条光线最多能够射多远即可。一个可行的思路就是首先判定这两个顶点及以前的光线是否在管子内,如果在管子内,再依次去判定光线最远能射到哪里。判定光线是否在管子内可以判定光线与每对顶点的纵截面的交点是否在管子内。
#include<stdio.h>
#include<string.h>
#define MAXD 50
#define zero 1e-8
int N;
double x[MAXD], y[MAXD], ans;
double fabs(double x)
{
return x < 0 ? -x : x;
}
int dcmp(double x)
{
if(fabs(x) < zero)
return 0;
if(x < 0)
return -1;
return 1;
}
void init()
{
int i, j, k;
for(i = 0; i < N; i ++)
scanf("%lf%lf", &x[i], &y[i]);
for(i = N; i < 2 * N; i ++)
x[i] = x[i - N], y[i] = y[i - N] - 1;
}
double det(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
}
double getx(double x1, double x2, double t1, double t2)
{
return (fabs(t2) * x1 + fabs(t1) * x2) / (fabs(t1) + fabs(t2));
}
double calculate(int k1, int k2)
{
int i, j, k;
double x1, x2, y1, y2, t = 0, t1, t2;
if(x[k1] > x[k2])
k = k1, k1 = k2, k2 = k;
x1 = x[k1], y1 = y[k1], x2 = x[k2], y2 = y[k2];
if(k2 > N)
k2 -= N;
for(i = 0; i < k2; i ++)
{
t1 = det(x2 - x1, y2 - y1, x[i] - x1, y[i] - y1);
t2 = det(x2 - x1, y2 - y1, x[i] - x1, y[i] - 1 - y1);
if(dcmp(t1) * dcmp(t2) > 0)
return x[1];
}
t = x2;
for(i = k2 + 1; i < N; i ++)
{
t1 = det(x2 - x1, y2 - y1, x[i - 1] - x1, y[i - 1] - y1);
t2 = det(x2 - x1, y2 - y1, x[i] - x1, y[i] - y1);
if(dcmp(t1) * dcmp(t2) < 0)
return t = getx(x[i - 1], x[i], t1, t2);
t1 = det(x2 - x1, y2 - y1, x[i - 1] - x1, y[i - 1] - 1 - y1);
t2 = det(x2 - x1, y2 - y1, x[i] - x1, y[i] - 1 - y1);
if(dcmp(t1) * dcmp(t2) < 0)
return t = getx(x[i - 1], x[i], t1, t2);
t1 = det(x2 - x1, y2 - y1, x[i] - x1, y[i] - y1);
if(dcmp(t1) * dcmp(t2) > 0)
return t;
t = x[i];
}
return t;
}
void solve()
{
int i, j, k;
double t;
ans = x[1];
for(i = 0; i < 2 * N; i ++)
for(j = i + 1; j < 2 * N; j ++)
if(j != i + N)
{
t = calculate(i, j);
if(t > ans)
ans = t;
}
if(dcmp(ans - x[N - 1]) >= 0)
printf("Through all the pipe.\n");
else
printf("%.2lf\n", ans);
}
int main()
{
for(;;)
{
scanf("%d", &N);
if(!N)
break;
init();
solve();
}
return 0;
}