Bee Breeding UVA - 808

选取图中1->5方向作为y轴,1->7方向作为x轴建立相应的坐标系,那么我们就可以直接从图形中发现在第2、4象限,对应的点到原点的距离为x和y坐标的绝对值的最大值,在1、3象限就是x的绝对值和y的绝对值之和,即可,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;

int a, b;

class Point{
public:
	int x, y;
	Point(int _x = 0, int _y = 0) :x(_x), y(_y){}
};

Point point[20005];
int amount = 1;

int dx[] = {-1,-1,0,1,1,0};
int dy[] = {0,1,1,0,-1,-1};

void getPoint(int dir,int l){
	while (l--){
		point[amount] = Point(point[amount-1].x+dx[dir],point[amount-1].y+dy[dir]);
		amount++;
	}
}

void Init(){
	point[amount++] = Point(0,0);
	point[amount++] = Point(1,-1);
	for (int i = 1; i <= 60; i++){
		for (int j = 0; j < 4; j++)
			getPoint(j, i);
		getPoint(4, i + 1);
		getPoint(5,i);
	}
}

int main(){
	Init();
	while (cin >> a >> b){
		if (a == 0 && b == 0) break;
		int x = point[a].x - point[b].x;
		int y = point[a].y - point[b].y;
		int ans;
		if (x > 0 && y < 0 || x < 0 && y>0)  ans = max(abs(x),abs(y));
		else ans = abs(x + y);
		cout << "The distance between cells " << a << " and " << b << " is " << ans << ".\n";
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值