UVA 808Bee Breeding(构造 建立坐标系)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=1e5;

int dx[5]={-1,0,1,1,0};
int dy[5]={1,2,1,-1,-2};
struct Point{
	int x;
	int y;
	Point()
	{
	}
	Point(int a,int b)
	{
		x=a;
		y=b;
	}
	
}p[N+20];
void init()
{
	int pn=1,x=0,y=0;;
	p[pn++]=Point(x,y);//按照编号建立坐标 
	y-=2;	
	p[pn++]=Point(x,y);

	for(int i=1;i<=60;i++)//画i个顺时针 
	{	
		for(int j=0;j<5;j++)
		{
			for(int k=1;k<=i;k++)//每个方向画k次 
			{
				x+=dx[j];
				y+=dy[j];
				p[pn++]=Point(x,y);
			}
		}
		y-=2;
		p[pn++]=Point(x,y);//偏移 
	
		for(int j=0;j<i;j++)//回到y轴 
		{
			x--;
			y--;
			p[pn++]=Point(x,y);
			//if(pn<32)
			//cout<<"@"<<pn-1<<" "<<(<<x<<","<<y<<)<<endl;
		}
	}
}
int main()
{
	int a,b;
	init();

	while(cin>>a>>b&&a&&b)
	{
		int ans;
		int x=abs(p[a].x-p[b].x);
		int y=abs(p[a].y-p[b].y);
		if(x>=y)//横向差较大 
		ans=x;//斜着走(每步坐标差可以-1)即可(原图中无法横着走) 
		else
		ans=x+(y-x)/2;//斜着走+往上走一步2个单位距离 
		printf("The distance between cells %d and %d is %d.
",a,b,ans);
	
	}
	return 0;
}


经验分享 程序员 微信小程序 职场和发展