思路:首先要建立坐标,具体作法见:http://www.cnblogs.com/xin-hua/p/3237096.html
然后将得到2坐标之差x,y;如果x,y同号,则相加,否则去最大.(要取绝对值的)
代码如下:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 50000
using namespace std;
struct dir
{
int x,y;
dir(){}
dir(int a,int b){
x=a;
y=b;
}
dir operator+(dir A){
return dir(A.x+x,A.y+y);
}
}an[];
dir solve(int n)
{
int k,m,i,j;
dir p;
k=((sqrt(12.0*n-3.0)-3.0)/6.0);
if(*k*(k+)+!=n) k++;
p.x=k;p.y=;
m=;
if(k>) m=*k-((*k*(k+)+)-n);
i=;
while(m){
for(j=;j<k;j++){
p=p+an[i];
m--;
if(m==)
break;
}
i++;
}
return p;
}
int main(){
int n,x,y,ans,m;
an[]=dir(-,);an[]=dir(-,);an[]=dir(,-);
an[]=dir(,-);an[]=dir(,);an[]=dir(,);
while(cin>>n>>m){
if(m==&&n==) break;
dir aa=solve(n);
dir bb=solve(m);
x=aa.x-bb.x;
y=aa.y-bb.y;
if((x<&&y<)||(x>&&y>))
ans=abs(x+y);
else ans=max(abs(x),abs(y));
printf("The distance between cells %d and %d is %d.\n",n,m,ans);
}
return ;
}