POJ 1870 Bee Breeding

这样的烂题我再也不想见到了!

比模拟题还讨厌!


题目大意:

依据题目给出的各个数的位置,求某两个数之间的位置。


解题思路:

由给出的位置能够找到一个规律,二维的六个象限的坐标系。计算坐标然后计算距离即可。


以下是代码:

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

#define eps 1e-8
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )

using namespace std;

void cal(int num,int &x,int &y)
{
    int n=0;
    while(1+3*n*(n+1)<num)n++;  //当不再第n圈范围内时
    if(n==0)
    {
        x=0;
        y=0;
        return;
    }                           //当属于第0圈直接返回
    num=num-1-3*n*(n-1);        //计算num是第n圈的第几个数
    int rou=(num-1)/n;          //计算属于第几象限
    int cnt=(num-1)%n+1;        //计算是第几象限的第几个数
    if(rou==0)x=cnt,y=n-cnt;
    else if(rou==1)x=n,y=-cnt;
    else if(rou==2)x=n-cnt,y=-n;
    else if(rou==3)x=-cnt,y=cnt-n;
    else if(rou==4)x=-n,y=cnt;
    else if(rou==5)x=cnt-n,y=n;
    return ;
}

int main()
{
    int a,b,xa,ya,xb,yb,ans;
    while(scanf("%d%d",&a,&b),a||b)
    {
        cal(a,xa,ya);
        cal(b,xb,yb);
        if((xb-xa)*(yb-ya)<=0)
        {
            ans=iabs(xa-xb);
            if(iabs(xa-xb)<iabs(ya-yb))ans+=iabs(ya-yb)-iabs(xa-xb);
        }
        else
        {
            ans=iabs(ya-yb)+iabs(xa-xb);
        }
        printf("The distance between cells %d and %d is %d.
",a,b,ans);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/cynchanpin/p/6884659.html