您的位置:首页 > 其它

UVa 10182 Bee Maja (规律&O(1)算法)

2013-11-22 13:12 1606 查看

10182 - Bee Maja

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123

Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive consists of many hexagonal honey combs where the honey is stored in.

But bee Maja has a problem. Willi told her where she can meet him, but because Willi is a male drone and Maja is a female worker they have different coordinate systems.

Maja's Coordinate System
Willi's Coordinate System
Maja who often flies directly to a special honey comb has laid an advanced two dimensional grid over the whole hive.Willi who is more lazy and often walks around just numbered the cells clockwise starting from 1 in the middle of the hive.


Help Maja to convert Willi's system to hers. Write a program which for a given honey comb number gives the coordinates in Maja's system.

Input Specification

The input file contains one or more integers which represent Willi's numbers. Each number stands on its own in a separate line, directly followed by a newline. The honey comb numbers are all less than 100 000.

Output Specification

You should output the corresponding Maja coordinates to Willi's numbers, each coordinate pair on a separate line.

Sample Input

1
2
3
4
5

Sample Output

0 0
0 1
-1 1
-1 0
0 -1


O(1)复杂度的思路:这道题如果只看左边那个图的话就会发现竖着的每一列纵坐标相同,而向左下方斜着的每一列纵坐标相同。但是这样并不能找出这些点的关系,其实换个角度,如果把左边的坐标在直角坐标系上画出来,就比较容易看出这是一个很有规律的一个图,只需要横纵坐标的加减就好了,至于加减多少那就看这个数所在的层数,层数可以根据x轴上的坐标的通项求出。

完整代码:

/*0.015s*/

#include<cstdio>
#include<cmath>

int main()
{
	int n, m, i, j;
	while (~scanf("%d", &m))
	{
		if (m == 1)
		{
			puts("0 0");
			continue;
		}
		--m;
		n = ceil((sqrt(12 * m + 9) - 3) / 6);
		if (3 * n * (n + 1) == m) printf("%d %d\n", n, 0);///x轴为起点
		else
		{
			--n;///前一个n
			m -= 3 * n * (n + 1);
			if (m <= n + 1) printf("%d %d\n", n - m + 1, m);
			else if (m <= (n + 1) << 1)
			{
				m -= (n + 1);
				printf("%d %d\n", -m, n + 1);
			}
			else if (m <= 3 * (n + 1))
			{
				m -= (n + 1) << 1;
				printf("%d %d\n", -(n + 1), n + 1 - m);
			}
			else if (m <= (n + 1) << 2)
			{
				m -= 3 * (n + 1);
				printf("%d %d\n", -(n - m + 1), -m);
			}
			else if (m <= 5 * (n + 1))
			{
				m -= (n + 1) << 2;
				printf("%d %d\n", m, -(n + 1));
			}
			else if (m <= 6 * (n + 1))
			{
				m -= 5 * (n + 1);
				printf("%d %d\n", n + 1, -(n + 1 - m));
			}
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: