SP1723 BMJ - Bee Maja

· · 题解

SP1723 BMJ - Bee Maja

分析

解题方法

这是一道比较简单的找规律题。

可以看到,题目中说明 n<10^5,且不确定有多少个 n,所以考虑 O(v) 预处理,O(1) 查询(其中,v 表示所有 n 的最大值)。

规律

观察可以发现,该蜜蜂是沿六边形不断转圈的,且当转到起始点前一格时,蜜蜂就会向下走一格。所以考虑找出每一个向方向走的时候 x 轴和 y 轴的变化。

::::info[以下是蜜蜂的移动规律(读者也可以自行找规律)] | 运动方向 | x 轴变化 | y 轴变化 | | :---: | :---: | :---: | | 下 | \ | +1 | | 左上 | -1 | \ | | 上 | \ | -1 | |右上|+1|-1| |右下|+1|\ | |下|\ |+1| ::::

代码

#include<bits/stdc++.h>
using namespace std;
const int dx[15]={-1,-1,0,1,1,0};
const int dy[15]={1,0,-1,-1,0,1};
int x[200005];
int y[200005];
vector<int> q;
int t;
int maxx;
int main()
{
    while(cin>>t)
    {
        q.push_back(t);
        maxx=max(maxx,t);
    }
    y[2]=1;
    int cnt=2;
    int cir=1;
    while(cnt<maxx)
    {
        for(int i=1; i<cir&&cnt<maxx; i++)
        {
            cnt++;
            x[cnt]=x[cnt-1]+dx[0];
            y[cnt]=y[cnt-1]+dy[0];
        }
        for(int j=1; j<5&&cnt<maxx; j++)
        {
            for(int i=1; i<=cir&&cnt<maxx; i++)
            {
                cnt++;
                x[cnt]=x[cnt-1]+dx[j];
                y[cnt]=y[cnt-1]+dy[j];
            }
        }
        for(int i=0; i<=cir&&cnt<maxx; i++)
        {
            cnt++;
            x[cnt]=x[cnt-1]+dx[5];
            y[cnt]=y[cnt-1]+dy[5];
        }
        cir++;
    }
    for(int i=0; i<q.size(); i++)
    {
        cout<<x[q[i]]<<" "<<y[q[i]]<<"\n";
    }

    return 0;
}