题意:求给出的两个数字所在表中的位置的距离。
以前想了好久,都没有想到好的方法,今天在DISCUSS里看到了比较好的解体方法,就把这题做了。
求点到顶部的距离X(层数),到左边的距离Y(层数),到右边的距离Z(层数)。当然也可以求到底边的距离,这样比较麻烦。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
struct node{
int x,y,z;
};
node oor(int k)
{
int x = sqrt(k-1.0);
int y = (k-x*x-1)/2;
int z = ((x+1)*(x+1)-k)/2;
node t;t.x=x,t.y=y,t.z=z;
return t;
}
int main()
{
freopen("in.txt","r",stdin);
int a,b;
while(~scanf("%d%d",&a,&b))
{
node t=oor(a),e=oor(b);
printf("%d\n",abs(t.x-e.x)+abs(t.y-e.y)+abs(t.z-e.z));
}
return 0;
}