POJ 1039 Pipe

时间:2022-10-14 22:12:54

题意:一根管子,中间有一些拐点,给出拐点的上坐标,下坐标为上坐标的纵坐标减1,管子不能透过光线也不能折射光线,问光线能射到最远的点的横坐标。

解法:光线射到最远处的时候一定最少经过两个拐点,枚举每两个顶点,判断最远光线射到的位置。代码姿势不够优美……都是眼泪啊

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
const double eps = 1e-, inf = 9999999.0;
struct point
{
double x, y;
} up[], down[];
int n;
double cross(point p1, point p2, point p3)
{
return (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y);
}
int judge(point a, point b)
{
for(int i = ; i < n; i++)
{
double tmp = cross(a, b, up[i]) * cross(a, b, down[i]);
if(tmp > eps)
return i;
}
return -;
}
int main()
{
while(~scanf("%d", &n) && n)
{
double ans = -inf;
int flag = ;
for(int i = ; i < n; i++)
{
double a, b;
scanf("%lf%lf", &a, &b);
up[i].x = down[i].x = a;
up[i].y = b, down[i].y = b - 1.0;
}
for(int i = ; i < n; i++)
for(int j = i + ; j < n; j++)
{
int tmp = judge(up[i], down[j]);
if(tmp >= )
{
double res;
if(tmp < j)
continue;
if(cross(up[i], down[j], up[tmp]) > )
{
double tmp1, tmp2;
tmp1 = cross(up[i], down[j], down[tmp - ]);
tmp2 = cross(up[i], down[j], down[tmp]);
res = (tmp2 * down[tmp - ].x - tmp1 * down[tmp].x) / (tmp2 - tmp1);
}
else
{
double tmp1, tmp2;
tmp1 = cross(up[i], down[j], up[tmp - ]);
tmp2 = cross(up[i], down[j], up[tmp]);
res = (tmp2 * up[tmp - ].x - tmp1 * up[tmp].x) / (tmp2 - tmp1);
}
ans = max(ans, res);
}
else
flag = ;
}
for(int i = ; i < n; i++)
for(int j = i + ; j < n; j++)
{
int tmp = judge(down[i], up[j]);
if(tmp >= )
{
double res;
if(tmp < j)
continue;
if(cross(down[i], up[j], up[tmp]) > )
{
double tmp1, tmp2;
tmp1 = cross(down[i], up[j], down[tmp - ]);
tmp2 = cross(down[i], up[j], down[tmp]);
res = (tmp2 * down[tmp - ].x - tmp1 * down[tmp].x) / (tmp2 - tmp1);
}
else
{
double tmp1, tmp2;
tmp1 = cross(down[i], up[j], up[tmp - ]);
tmp2 = cross(down[i], up[j], up[tmp]);
res = (tmp2 * up[tmp - ].x - tmp1 * up[tmp].x) / (tmp2 - tmp1);
}
ans = max(ans, res);
}
else
flag = ;
}
if(flag)
puts("Through all the pipe.");
else
printf("%.2f\n", ans);
}
return ;
}