标签:简单模拟
1.题目描述:点击打开链接
2.解题思路:本题利用构造法解决。本题我思考了很久,但迟迟没有很好的思路。最后才意识到只需要按照蜂巢的构造在xOy坐标系中画出这些点即可求解了。。蜂巢的画法不难通过观察发现,类似于螺旋结构。把(0,0)安排为第一个点,纵向距离和横向距离都为2个单位距离。这样便可以将图形中所有的店全部画出来。经过计算不难知道,只需要循环60次即可将10000以内的点表示出来。
这样输入的相当于是点的序号,由点的坐标不难得到两点的横向距离x和纵向距离y。当y≤x时,最短距离为x。否则,由于纵向和横向都间距2个单位,因此实际上最短距离为x,y的平均值。
3.代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;
const int N = 20005;
const int dx[] = { -1, 0, 1, 1, 0 };
const int dy[] = { 1, 2, 1, -1, -2 };
struct Point
{
int x, y;
Point(){}
Point(int x, int y)
{
this->x = x;
this->y = y;
}
}p[N];
int a, b;
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++)
{
for (int j = 0; j < 5; j++)//按顺序画5个方向
for (int k = 0; k < i; k++)//沿着dx[j],dy[j]方向画i次
{
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);
}
}
}
int main()
{
//freopen("t.txt", "r", stdin);
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 (y <= x)printf("%d.\n", x);//由于在图中横向的两点也是间隔2个单位,因此直接就是x
else printf("%d.\n", x + (y - x) / 2);//由于两点的横向,纵向都间隔两个单位,因此要取平均值
}
return 0;
}标签:简单模拟
原文地址:http://blog.csdn.net/u014800748/article/details/45195689