题意:输入两个格子的编号a和b(a,b<=10000),求最短距离
思路:建坐标系找规律,参考他人思路思路链接
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define mod 1000000007;
using namespace std;
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
const int maxn = 20000 + 2;
struct node
{
int x,y;
node(){}
node(int a,int b)
{
x = a;
y = b;
}
}p[maxn];
int dx[] = {-1,0,1,1,0};
int dy[] = {1,2,1,-1,-2};
void init()
{
int x = 0,y = 0,num = 1;
p[num++] = node(x,y);
y -= 2;
p[num++] = node(x,y);
for(int i = 1;i <= 60;i++)
{
for(int j = 0;j < 5;j++)
{
for(int k = 0;k < i;k++)
{
x += dx[j],y += dy[j];
p[num++] = node(x,y);
}
}
y -= 2;
p[num++] = node(x,y);
for(int j = 0;j < i;j++)
{
x--,y--;
p[num++] = node(x,y);
}
}
}
int main()
{
int a,b;
init();
while(scanf("%d%d",&a,&b) && a + b)
{
int x = abs(p[a].x - p[b].x);
int y = abs(p[a].y - p[b].y);
printf("The distance between cells %d and %d is ",a,b);
if(x >= y) printf("%d.\n",x);
else printf("%d.\n",x + (y - x) / 2);
}
return 0;
}