poj 1039

实践告诉我们有小数的时候选择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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值