选取图中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;
}