UVALive - 7528 Beehive
2017-05-05 21:23
375 查看
题目链接
分析:
我们可以建一个坐标系
竖线右边+X,左边-X,一列为一;纵方向半个六边形高度为一;
假设坐标(-2, 2)到(3,4),那么让(-2, 2)的横坐标先变成3这样至少三步;如果纵坐标的高度差小于等于横坐标高度差,那么高度差可以在横坐标平移的的过程中弥补,否则还需要纵坐标平移。
代码:
分析:
我们可以建一个坐标系
竖线右边+X,左边-X,一列为一;纵方向半个六边形高度为一;
假设坐标(-2, 2)到(3,4),那么让(-2, 2)的横坐标先变成3这样至少三步;如果纵坐标的高度差小于等于横坐标高度差,那么高度差可以在横坐标平移的的过程中弥补,否则还需要纵坐标平移。
代码:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <vector> #include <queue> #include <deque> #include <stack> #include <set> #include <cmath> using namespace std; const int d[] = {4, 2}; int sum[200], num[200]; int init() { sum[1] = 0; int t = 1, cnt = 2; while (true) { num[cnt - 1] = sum[cnt] = t; cnt ++; t += d[cnt & 1]; if (cnt > 100) break; } for (int i = 1; i < cnt; i ++) sum[i] += sum[i - 1]; return cnt; } void work(int x, int len, int &xx, int &yy) { int f = (int)(lower_bound(sum + 1, sum + len, x) - sum - 1); int k = x - sum[f]; int d = (f + 1) / 2; if (k <= d || k >= num[f] - d + 1) { if (k <= d) { xx = 1 - f; yy = (k - 1) * 2 + 1; if (f % 2 == 0) yy ++; } else { xx = f - 1; int tmpk = num[f] - k; yy = tmpk * 2 + 1; if (f % 2 == 0) yy ++; } } else { if (k > num[f] / 2) { int tmpk = k - num[f] / 2; xx = tmpk - 1; int rnk = num[f] - k - d; yy = rnk + d * 2; if (f % 2 == 0) yy ++; } else { xx = k - num[f] / 2 - 1; int rnk = k - d - 1; yy = rnk + d * 2; if (f % 2 == 0) yy ++; } } } int main() { int cnt = init(); int a, b; while (~scanf("%d%d", &a, &b)) { if (a == 0 && b == 0) break; int x1, x2, y1, y2; work(a, cnt, x1, y1); work(b, cnt, x2, y2); int dx = abs(x1 - x2); int dy = abs(y1 - y2); if (dy > dx) printf("%d\n", (dy - dx) / 2 + dx); else printf("%d\n", dx); } return 0; }
相关文章推荐
- Beehive UVALive - 7528 (找规律+数学思维)
- UVALive-7528-Beehive
- UVALive 7528(2015德黑兰区域赛) Beehive
- Beehive UVALive - 7528
- Beehive UVALive - 7528
- UVALive - 5052 Genome Evolution 贪心
- POJ2109 ZOJ2486 UVA113 UVALive3098 Power of Cryptography【数学】
- UVa Live-3357 Pinary(斐波那契+找规律)
- UVALive 5865 Finding Bottleneck Shorstet Paths
- UVALive3637(01背包+贪心)
- UVALive 3305 Tour(DP)
- Exploring Pyramids UVALive - 3516 题解
- UVALive 6604 Airport Sort(逆序对+贪心)
- Repeated Substrings(UVAlive 6869)
- UVAlive 4763 Sudoku Extension【DLX】
- UVALive 3135--Argus+自己定义优先队列的优先规则
- UVALive 6257 Chemist's vows
- UVALive 4639 - Separate Points
- uvalive 4329 Ping pong (树状数组)
- UVa Live 7040 (二项式反演+线性求逆元)