• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

poj2265

斜坐标系,注意找规律。说明在代码里。


#include <iostream>
#include <math.h>

using namespace std;

#define eps 1e-6
#define zero(x) (((x)>0?(x):-(x))<eps)

int direct[6][2] = { { 0, 1 }, { -1, 1 }, { -1, 0 }, { 0, -1 }, { 1, -1 }, { 1, 0 } };//斜坐标系的六个方向

int main()
{
    int x;
    while (cin >> x)
    {
        double n = (sqrt(12 * x - 3)*1.0 - 3) / 6;
        int p = zero(n - ceil(n)) ? (int) n : ceil(n);
        int t = x - 3 * p * p + 3 * p - 1;
        int xcord, ycord;
        //x equals the n loops plus t.
        //cout << (int) p << ' ' << t << endl;
        xcord = p - 1; ycord = 0;
        while (1)
        {
            if (t == 0) goto L1;//t==0就可以撤出来了~
            xcord += direct[0][0], ycord += direct[0][1];//先向direct[0]走一步
            t--;
            if (t == 0) goto L1;
            for (int i = 1; i <= p - 1; i++)
            {
                xcord += direct[1][0], ycord += direct[1][1];//再向direct[1]走p-1步
                t--;
                if (t == 0) goto L1;
            }
            for (int i = 2; i <= 6; i++)
            {
                for (int j = 1; j <= p; j++)
                {
                    xcord += direct[i % 6][0], ycord += direct[i % 6][1];//然后向其他剩余的五个方向各走p步
                    t--;
                    if (t == 0) goto L1;
                }
            }
        }
    L1: cout << xcord << ' ' << ycord << endl;
    }
}


posted @ 2013-10-12 23:02  Class Xman  阅读(144)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3