码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1039 Pipe

时间:2015-03-04 18:44:41      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

题意:一根管子,中间有一些拐点,给出拐点的上坐标,下坐标为上坐标的纵坐标减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-8, inf = 9999999.0;
struct point
{
    double x, y;
} up[25], down[25];
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 = 0; i < n; i++)
    {
        double tmp = cross(a, b, up[i]) * cross(a, b, down[i]);
        if(tmp > eps)
            return i;
    }
    return -1;
}
int main()
{
    while(~scanf("%d", &n) && n)
    {
        double ans = -inf;
        int flag = 0;
        for(int i = 0; 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 = 0; i < n; i++)
            for(int j = i + 1; j < n; j++)
            {
                int tmp = judge(up[i], down[j]);
                if(tmp >= 0)
                {
                    double res;
                    if(tmp < j)
                        continue;
                    if(cross(up[i], down[j], up[tmp]) > 0)
                    {
                        double tmp1, tmp2;
                        tmp1 = cross(up[i], down[j], down[tmp - 1]);
                        tmp2 = cross(up[i], down[j], down[tmp]);
                        res = (tmp2 * down[tmp - 1].x - tmp1 * down[tmp].x) / (tmp2 - tmp1);
                    }
                    else
                    {
                        double tmp1, tmp2;
                        tmp1 = cross(up[i], down[j], up[tmp - 1]);
                        tmp2 = cross(up[i], down[j], up[tmp]);
                        res = (tmp2 * up[tmp - 1].x - tmp1 * up[tmp].x) / (tmp2 - tmp1);
                    }
                    ans = max(ans, res);
                }
                else
                    flag = 1;
            }
        for(int i = 0; i < n; i++)
            for(int j = i + 1; j < n; j++)
            {
                int tmp = judge(down[i], up[j]);
                if(tmp >= 0)
                {
                    double res;
                    if(tmp < j)
                        continue;
                    if(cross(down[i], up[j], up[tmp]) > 0)
                    {
                        double tmp1, tmp2;
                        tmp1 = cross(down[i], up[j], down[tmp - 1]);
                        tmp2 = cross(down[i], up[j], down[tmp]);
                        res = (tmp2 * down[tmp - 1].x - tmp1 * down[tmp].x) / (tmp2 - tmp1);
                    }
                    else
                    {
                        double tmp1, tmp2;
                        tmp1 = cross(down[i], up[j], up[tmp - 1]);
                        tmp2 = cross(down[i], up[j], up[tmp]);
                        res = (tmp2 * up[tmp - 1].x - tmp1 * up[tmp].x) / (tmp2 - tmp1);
                    }
                    ans = max(ans, res);
                }
                else
                    flag = 1;
            }
        if(flag)
            puts("Through all the pipe.");
        else
            printf("%.2f\n", ans);
    }
    return 0;
}

 

POJ 1039 Pipe

标签:

原文地址:http://www.cnblogs.com/Apro/p/4313839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!