UVA 808 - Bee Breeding(数论+坐标系)

题目链接:808 - Bee Breeding

题意:给定图中蜂窝两点,求最短距离
思路:建坐标系,然后根据图中走的顺序把每个点的坐标求出来,然后利用坐标去求最小距离即可
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

const int N = 20005;
const int d[5][2] = {{-1, 1}, {0, 2}, {1, 1}, {1, -1}, {0, -2}};

struct Point {
	int x, y;
	Point() {}
	Point(int _x, int _y) {
		x = _x; y = _y;
	}
} p[N];

int a, b;

void init() {
	int pn = 1, x = 0, y = 0;
	p[pn++] = Point(x, y);
	y -= 2;
	p[pn++] = Point(x, y);
	for (int i = 1; i <= 60; i++) {
		int j, k;
		for (j = 0; j < 5; j++) {
			for (k = 0; k < i; k++) {
				x += d[j][0]; y += d[j][1];
				p[pn++] = Point(x, y);
			}
		}
		y -= 2;
		p[pn++] = Point(x, y);
		for (j = 0; j < i; j++) {
			x--; y--;
			p[pn++] = Point(x, y);
		}
	}
}

int main() {
	init();
	while (~scanf("%d%d", &a, &b) && a || b) {
		int x = abs(p[a].x - p[b].x);
		int y = abs(p[a].y - p[b].y);
		printf("The distance between cells %d and %d is ", a, b);
		if (y <= x) printf("%d.\n", x);
		else {
			printf("%d.\n", x + (y - x) / 2);
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值