先把蜂巢快递柜画出来:
__ __ __ __
__/ \__/ \__/ \__/ \__
__/ \__/ \__/53\__/ \__/ \__
/ \__/ \__/52\__/54\__/ \__/ \
\__/ \__/51\__/31\__/55\__/ \__/
/ \__/50\__/30\__/32\__/56\__/ \
\__/49\__/29\__/15\__/33\__/57\__/
/ \__/28\__/14\__/16\__/34\__/ \
\__/48\__/13\__/ 5\__/17\__/58\__/
/..\__/27\__/ 4\__/ 6\__/35\__/ \
\__/47\__/12\__/ 1\__/18\__/59\__/
/..\__/26\__/ 3\__/ 7\__/36\__/ \
\__/46\__/11\__/ 2\__/19\__/60\__/
/..\__/25\__/10\__/ 8\__/37\__/ \
\__/45\__/24\__/ 9\__/20\__/61\__/
/..\__/44\__/23\__/21\__/38\__/ \
\__/70\__/43\__/22\__/39\__/62\__/
/ \__/69\__/42\__/40\__/63\__/ \
\__/ \__/68\__/41\__/64\__/ \__/
/ \__/ \__/67\__/65\__/ \__/ \
\__/ \__/ \__/66\__/ \__/ \__/
\__/ \__/ \__/ \__/ \__/
\__/ \__/ \__/ \__/
显然,算一下点坐标即可
设
_____
_____/ 0,2 \_____
/ 1,-1\_____/ 1,1 \
\_____/ 0,0 \_____/
/-1,-1\_____/ 1,-1\
\_____/ 0,-2\_____/
\_____/
然后计算两个数的距离时,我们可以计算两个数的位置横坐标差位x,纵坐标差位y,当x < y的时候,按照斜线走,走到相同列的时候就可以直接向下走,一直到v,在向下走的时候,一步可以走坐标中的2的距离。
当x>=y的时候,可以先斜线走走到相同的行,然后横着走,一直找到v,在横着走的时候,一次只能走一个坐标。
代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct point{
int x,y;
}p,q,a[10010];
int s[6][2]={{-1,-1},{-1,1},{0,2},{1,1},{1,-1},{0,-2}};
void init(){
a[1].x=a[1].y=0;
int k,i,j,cnt=1;
for(k=2;cnt<10000;k++){
p.x=a[cnt].x;
p.y=a[cnt].y-2;
a[++cnt]=p;
for(i=0;i<6;i++){
for(j=0;j<k-1;j++){
if(i==0&&j==k-2)continue;
p.x+=s[i][0];
p.y+=s[i][1];
a[++cnt]=p;
if(cnt==10000)break;
}
if(cnt==10000)break;
}
}
}
int main(){
init();
int u,v,x,y,ans;
while(scanf("%d%d",&u,&v)){
if(u+v==0)return 0;
x=abs(a[u].x-a[v].x);
y=abs(a[u].y-a[v].y);
ans=0;
if(x<y){
ans=x+(y-x)/2;
}
else ans=y+(x-y);
printf("The distance between cells %d and %d is %d.\n",u,v,ans);
}
return 0;
}
标签:__,_____,..,int,POJ,坐标,1870,include
From: https://www.cnblogs.com/zan-mei-tai-yang/p/18395210