UVa1531 - Problem Bee

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.

graphic of a honeycomb

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 of 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, to the nearest 0.001 centimeters.

Sample Input

1.0 -3.2 2.2 3.3 0
9 1 4 5 1
0.1 0.09 0 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-6;
const double SQRT3 = sqrt((double)3);
const int dir[7][2] = {{0, 0}, {-1, 1}, {0, 2}, {1, 1}, {1, -1}, {0, -2}, {-1, -1}};

struct Point
{
    double x, y;
};

double r, xa, ya, xb, yb;
double stepx, stepy;

bool input();
Point findFirstSection(double x, double y);
Point find(double x, double y);
void solve();

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

    while (input()) {
        solve();
    }
    return 0;
}

bool input()
{
    if (scanf("%lf%lf%lf%lf%lf", &r, &xa, &ya, &xb, &yb) != 5 ||
            !(r || xa || ya || xb || yb)) return false;

    stepx = 1.5 * r;
    stepy = SQRT3 / 2 * r;

    return true;
}

Point findFirstSection(double x, double y)
{
    int dx = (int)(x / stepx);
    int dy = (int)(y / stepy);
    if (dx & 1) {
        if (!(dy & 1)) dy++;
    } else {
        if (dy & 1) dx++;
    }

    double Min = 0x3f3f3f3f;
    Point ans;

    for (int i = 0; i < 7; i++) {
        double tmpx = stepx * (dx + dir[i][0]);
        double tmpy = stepy * (dy + dir[i][1]);
        double tmp = sqrt(pow(tmpx - x, 2) + pow(tmpy - y, 2));

        if (tmp < Min) {
            ans.x = tmpx;
            ans.y = tmpy;
            Min = tmp;
        }
    }

    return ans;
}

Point find(double x, double y)
{
    Point ans;

    if (x > 0 && y > 0) {
        ans = findFirstSection(x, y);
    } else if (x < 0 && y > 0) {
        ans = findFirstSection(-x, y);
        ans.x = -ans.x;
    } else if (x < 0 && y < 0) {
        ans = findFirstSection(-x, -y);
        ans.x = -ans.x;
        ans.y = -ans.y;
    } else {
        ans = findFirstSection(x, -y);
        ans.y = -ans.y;
    }

    return ans;
}

void solve()
{
    Point a = find(xa, ya);
    Point b = find(xb, yb);
    double dis;

    if (fabs(a.x - b.x) < EPS && fabs(a.y - b.y) < EPS) {
        dis = sqrt(pow(xa - xb, 2) + pow(ya - yb, 2));
        printf("%.3lf\n", dis);
        return;
    }
	 
	 int x1 = (int)(fabs(a.x - b.x) / stepx + 0.5);
	 int x2 = (int)(fabs(a.y - b.y) / stepy + 0.5);
	 int d;
	 
	 if (x1 >= x2) {
		 d = x1;
	 } else {
		 d = x1 + (x2 - x1) / 2;
	 }
    dis = sqrt(pow(a.x - xa, 2) + pow(a.y - ya, 2)) + sqrt(pow(b.x - xb, 2) + pow(b.y - yb, 2)) + d * SQRT3 * r;

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kgduu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值