https://vjudge.net/problem/UVA-808
#include<cmath> #include<cstdio> #include<algorithm> using namespace std; int dx[6]={0,-1,-1,0,1,1}; int dy[6]={-1,-1,0,1,1,0}; int s[6]={2,1,2,2,2,2}; int x[10001],y[10001]; int main() { y[2]=-1; x[3]=-1; y[3]=-1; x[4]=-1; y[5]=1; x[6]=1; y[6]=1; int nowd=0,nows=1,nowx=1,nowy=1; for(int now=7;now<=10000;now++) { if(nows>s[nowd]) nows=1,nowd++; if(nowd>=6) { nowd=0; for(int i=0;i<6;i++) s[i]++; } nowx+=dx[nowd]; nowy+=dy[nowd]; x[now]=nowx; y[now]=nowy; nows++; } int a,b,aa,bb; while(scanf("%d%d",&aa,&bb)!=EOF) { a=aa; b=bb; if(!a) return 0; if(x[a]>x[b]) swap(a,b); if(x[a]!=x[b] && y[a]<y[b]) printf("The distance between cells %d and %d is %d.\n",aa,bb,max(x[b]-x[a],y[b]-y[a])); else printf("The distance between cells %d and %d is %d.\n",aa,bb,abs(y[a]-y[b])+x[b]-x[a]); } }