UVALive

来源:互联网 发布:小米 网络诊断怎么关闭 编辑:程序博客网 时间:2024/06/07 13:23

题目链接

分析:
我们可以建一个坐标系
图
竖线右边+X,左边-X,一列为一;纵方向半个六边形高度为一;
假设坐标(-2, 2)到(3,4),那么让(-2, 2)的横坐标先变成3这样至少三步;如果纵坐标的高度差小于等于横坐标高度差,那么高度差可以在横坐标平移的的过程中弥补,否则还需要纵坐标平移。

代码:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cstdlib>#include <vector>#include <queue>#include <deque>#include <stack>#include <set>#include <cmath>using namespace std;const int d[] = {4, 2};int sum[200], num[200];int init() {    sum[1] = 0;    int t = 1, cnt = 2;    while (true) {        num[cnt - 1] = sum[cnt] = t;        cnt ++;        t += d[cnt & 1];        if (cnt > 100) break;    }    for (int i = 1; i < cnt; i ++)        sum[i] += sum[i - 1];    return cnt;}void work(int x, int len, int &xx, int &yy) {    int f = (int)(lower_bound(sum + 1, sum + len, x) - sum - 1);    int k = x - sum[f];    int d = (f + 1) / 2;    if (k <= d || k >= num[f] - d + 1) {        if (k <= d) {            xx = 1 - f;            yy = (k - 1) * 2 + 1;            if (f % 2 == 0) yy ++;        }        else {            xx = f - 1;            int tmpk = num[f] - k;            yy = tmpk * 2 + 1;            if (f % 2 == 0) yy ++;        }    }    else {        if (k > num[f] / 2) {            int tmpk = k - num[f] / 2;            xx = tmpk - 1;            int rnk = num[f] - k - d;            yy = rnk + d * 2;            if (f % 2 == 0) yy ++;        }        else {            xx = k - num[f] / 2 - 1;            int rnk = k - d - 1;            yy = rnk + d * 2;            if (f % 2 == 0) yy ++;        }    }}int main() {    int cnt = init();    int a, b;    while (~scanf("%d%d", &a, &b)) {        if (a == 0 && b == 0) break;        int x1, x2, y1, y2;        work(a, cnt, x1, y1);        work(b, cnt, x2, y2);        int dx = abs(x1 - x2);        int dy = abs(y1 - y2);        if (dy > dx) printf("%d\n", (dy - dx) / 2 + dx);        else printf("%d\n", dx);    }    return 0;}
0 0
原创粉丝点击