首页 > 代码库 > 【hdu1030】“坐标表示法”

【hdu1030】“坐标表示法”

http://acm.hdu.edu.cn/showproblem.php?pid=1030

算法:以顶点为原点,建立坐标系,一个数可以唯一对应一个三元组(x, y, z),从任意一个点出发走一步,刚好有三种情况,分别对应x, y, z变化1,而其它两个坐标保持不变。因此,求出两个点的坐标分别为(x1, y1, z1), (x2, y2, z2);则它们之间的距离为|x1 - x2| + |y1 - y2| + |z1 - z2|。

代码如下:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <map> 7 #include <vector> 8 #include <stack> 9 #include <string>10 #include <ctime>11 #include <queue>12 #define mem0(a) memset(a, 0, sizeof(a))13 #define mem(a, b) memset(a, b, sizeof(a))14 #define lson l, m, rt << 115 #define rson m + 1, r, rt << 1 | 116 #define eps 0.000000117 #define lowbit(x) ((x) & -(x))18 #define memc(a, b) memcpy(a, b, sizeof(b))19 #define x_x(a) ((a) * (a))20 #define LL long long21 #define DB double22 #define pi 3.1415926535923 #define MD 1000000724 #define INF maxNum25 #define max(a, b) ((a) > (b)? (a) : (b))26 using namespace std;27 struct Point {28         int x, y, z;29         Point(int n) {30                 int xx = (int)sqrt(n + 0.5);31                 if(xx * xx  < n) xx++;32                 y = xx;33                 z = x = y;34                 int p = (y - 1) * (y - 1);35                 x -= (n - p) / 2;36                 p = y * y + 1;37                 z -= (p - n) / 2;38         }39 };40 int main()41 {42         //freopen("input.txt", "r", stdin);43         int a, b;44         while(~scanf("%d%d", &a, &b)) {45                 Point x(a), y(b);46                 int ans = abs(x.x - y.x) + abs(x.y - y.y) + abs(x.z - y.z);47                 printf("%d\n", ans);48         }49         return 0;50 }
View Code

 

【hdu1030】“坐标表示法”