poj 1518 Problem Bee

Problem Bee
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 286 Accepted: 150

Description

Imagine a perfectly formed honeycomb, spanning the infinite Cartesian plane. It is an interlocking grid composed of congruent equilateral hexagons. One hexagon is located so that its center is at the origin and has two corners on the X-axis. A bee must be very careful about how it travels in order not to get lost in the infinite plane. To get from an arbitrary point A to another arbitrary point B, it will first head from A to the exact center of the hexagon in which A is located. Then, it will travel in a straight line to the exact center of an adjacent hexagon. It will move from center to adjacent center until it has reached the hexagon containing point B. At the destination hexagon, it will move from the center to point B. In all cases the bee will take a path of minimal distance that obeys the rules. The figure below demonstrates one possible minimal path from point A to point B. 

Input

Input will be in the form of 5 floating point numbers per line. The first number will be the length, in centimeters, of the sides of the hexagons. The next two numbers will be the x and y coordinates of point A, followed by the x and y coordinates for point B. The input will be terminated by a line containing five zeroes. Neither point A nor point B will ever be exactly on a border between hexagons.

Output

For each line of the input, output the minimum length of a path from point A to point B, in centimeters, to the nearest .001 centimeters.

Sample Input

1.0 -3.2 2.2 3.3 0
9 1 4 5 1
0.1 .09 0 .21 0
0 0 0 0 0

Sample Output

7.737
5.000
0.526
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const double EPS = 1e-9;
const double sqrt3 = sqrt(3);

double r, xa, ya, xb, yb;

void solve();

//第二象限的转换
void transform(double xa, double ya, double &x, double &y);
//任意象限的转换
void transform2(double xa, double ya, double &x, double &y);
double dis(double x1, double y1, double x2, double y2);

int main()
{
#ifndef ONLINE_JUDGE
    freopen("e:\\uva_in.txt", "r", stdin);
#endif

    while (scanf("%lf%lf%lf%lf%lf", &r, &xa, &ya, &xb, &yb) == 5) {
        if (fabs(r) < EPS && fabs(xa) < EPS && fabs(ya) < EPS && fabs(xb) < EPS && fabs(yb) < EPS) break;

        solve();
    }
    return 0;
}

void solve()
{
    double x1, y1, x2, y2;
    double ans = 0;

    transform2(xa, ya, x1, y1);
    //printf("x1 = %lf, y1 = %lf\n", x1, y1);
    transform2(xb, yb, x2, y2);
    //printf("x2 = %lf, y2 = %lf\n", x2, y2);



    if (fabs(x1 - x2) < EPS && fabs(y1 - y2) < EPS) {
        ans = dis(xa, ya, xb, yb);
        printf("%.3lf\n", ans);
        return;
    }

    int x = (int)(fabs(x1 - x2) / (1.5 * r));
    int y = (int)(fabs(y1 - y2) / (0.5 * sqrt3 * r));

    //printf("x = %d, y = %d\n", x, y);
    ans = max(x, y) * sqrt3 * r;
    ans += dis(x1, y1, xa, ya);
    ans += dis(x2, y2, xb, yb);

    printf("%.3lf\n", ans);

}

void transform(double xa, double ya, double &x1, double &y1)
{
    int n1 = fabs(xa) / r;
    int n2 = fabs(ya) / (sqrt3 / 2 * r);

    double x, y;

    //printf("n1 = %d, n2 = %d\n", n1, n2);

    if (n1 % 3 == 0 || n1 % 3 == 2) {
        if (n1 % 3 == 0) {
            x = fabs(xa) - n1 / 3 * 3 * r - r / 2;
            //printf("x=%lf\n", x);
            if (x <= 0) {
                x1 = n1 / 3 * 3 * r;
                y1 = (n2 + 1) / 2 * sqrt3;
            } else {

                if (n2 % 2 == 0) {
                    x1 = r / 2 - x;
                    y1 = fabs(ya) - n2 * sqrt3 / 2;

                    if (x1 * sqrt3 > y1) {
                        //printf("here\n");
                        x1 = n1 / 3 * 3 * r;
                        y1 = (n2 + 1) / 2 * sqrt3 * r;
                    } else {
                        x1 = n1 / 3 * 3 * r + r + r / 2;
                        y1 = (n2 + 1) / 2  * sqrt3 * r + sqrt3 / 2 * r;
                    }
                } else {
                    y1 = fabs(ya) - n2 * sqrt3 / 2 * r;
                    x1 = x;
                    if (x1 * sqrt3 < y1) {
                        x1 = n1 / 3 * 3 * r;
                        y1 = (n2 + 1) / 2 * sqrt3 * r;
                    } else {
                        x1 = n1 / 3 * 3 * r + r + r / 2;
                        y1 = (n2 + 1) / 2 * sqrt3 * r - sqrt3 * r / 2;
                    }
                }
            }
        } else {
            x = fabs(xa) - n1 / 3 * 3 * r - 2 * r;

            //printf("x=%lf\n", x);
            if (x > sqrt3 / 2 * r) {
                x1 = n1 / 3 * 3 * r;
                y1 = (n2 + 1) / 2 * sqrt3 * r;
            } else {
                y = fabs(ya) - n2 * sqrt3 / 2 * r;
                if (n2 % 2 == 1) {
                    x1 = r / 2 - x;
                    y1 = fabs(ya) - n2 * sqrt3 / 2 * r;

                    if (x1 * sqrt3 < y1) {
                        x1 = (n1 + 1) / 3 * 3 * r;
                        y1 = (n2 + 1) / 2 * sqrt3 * r;
                    } else {
                        x1 = (n1 + 1) / 3 * 3 * r - r - r / 2;
                        y1 = (n2 + 1) / 2 * sqrt3 * r - sqrt3 / 2 * r;
                    }
                } else {
                    //printf("ok\n");
                    y1 = fabs(ya) - n2 * sqrt3 / 2 * r;
                    if (x * sqrt3 > y1) {
                        x1 = (n1 + 1)/ 3 * 3 * r;
                        y1 = (n2 + 1) / 2 * sqrt3 * r;
                    } else {
                        x1 = (n1 + 1)/ 3 * 3 * r - 1.5 * r;
                        y1 = (n2 + 1) / 2  * sqrt3 * r + sqrt3 / 2 * r;
                    }
                }
            }
        }
    } else {
        x1 = n1 / 3 * 3 * r + 1.5 * r;
        y1 = sqrt3 / 2 * r + n2 / 2 * sqrt3 * r;
    }

    x1 = -x1;
}

void transform2(double xa, double ya, double &x, double &y)
{
    double tmpx, tmpy;

    transform(xa, ya, tmpx, tmpy);

    if (xa < 0 && ya > 0) {
        x = tmpx, y = tmpy;
    } else if (xa > 0 && ya > 0){
        x = -tmpx, y = tmpy;
    } else if (xa < 0 && ya < 0) {
        x = tmpx, y = -tmpy;
    } else {
        x = -tmpx, y = -tmpy;
    }
}

double dis(double x1, double y1, double x2, double y2)
{
    double x = x2 - x1;
    double y = y2 - y1;

    return sqrt(x * x + y * y);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值